DrawerLayout+Tablayout+PullTofresh+Fragment(Viewpage)

来源:互联网 发布:手机淘宝看自己好评率 编辑:程序博客网 时间:2024/06/01 08:18

一,先在  manifrsts  写权限

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

二,在  <application  里面写


android:name=".MApp"


三,添加依赖

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'compile 'com.android.support:design:26+'compile 'com.google.code.gson:gson:2.2.4'compile 'com.github.userswlwork:pull-to-refresh:1.0.0'compile 'com.youth.banner:banner:1.4.9'

注意:有gson的依赖,就不用导Gson包


四,布局
main:里布局

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/dv"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.bawei.chenkai.chenkai20171023.MainActivity">    <ListView        android:id="@+id/lv"        android:layout_width="240dp"        android:layout_height="match_parent"        android:choiceMode="singleChoice"        android:divider="#000"        android:layout_gravity="start"        android:dividerHeight="1dp"        android:background="#007efe">    </ListView>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <FrameLayout            android:id="@+id/fl_main"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="9" />        <RadioGroup            android:id="@+id/rg"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:gravity="center"            android:orientation="horizontal">            <RadioButton                android:id="@+id/but1"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:gravity="center"                android:text="首页" />            <RadioButton                android:id="@+id/but2"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:gravity="center"                android:text="发现" />            <RadioButton                android:id="@+id/but3"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:gravity="center"                android:text="下载" />            <RadioButton                android:id="@+id/but4"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:gravity="center"                android:text="我的" />        </RadioGroup>    </LinearLayout></android.support.v4.widget.DrawerLayout>


3,fragment01 :里布局


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.design.widget.TabLayout        android:id="@+id/tl_titles"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1">    </android.support.design.widget.TabLayout>    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="9"/></LinearLayout>
4,fragment02 :里布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"android:background="#44ff44">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="发现"        android:textSize="30sp"        /></LinearLayout>
5,fragment03 :里布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="下载"        android:textSize="30sp"        /></LinearLayout>
6,fragment04 :里布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff3660">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我的"        android:textSize="30sp"        /></LinearLayout>
7,item0 :里布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/plv"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
8,listview :里布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
一,MainActivity :主方法里
public class MainActivity extends AppCompatActivity {    private RadioGroup rg;    private ArrayList<Fragment> list = new ArrayList<Fragment>();    private FragmentManager supportFragmentManager;    private DrawerLayout drawerLayout;    private ListView lv;    ArrayList<String> list1 = new ArrayList<String>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initFragment();        //初始化组件        rg = (RadioGroup) findViewById(R.id.rg);        drawerLayout = (DrawerLayout) findViewById(R.id.dv);        lv = (ListView) findViewById(R.id.lv);        getData();        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                drawerLayout.closeDrawer(lv);            }        });        supportFragmentManager = getSupportFragmentManager();        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();        fragmentTransaction.add(R.id.fl_main, list.get(0)).commit();        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {                switch (i) {                    case R.id.but1:                        supportFragmentManager.beginTransaction().replace(R.id.fl_main, new Fragment01()).commit();                        break;                    case R.id.but2:                        getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment02()).commit();                        break;                    case R.id.but3:                        getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment03()).commit();                        break;                    case R.id.but4:                        getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment04()).commit();                }            }        });    }    private void getData() {        for (int i = 0; i < 10; i++) {            list1.add("item" + i);        }        //自定义适配器        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list1);        lv.setAdapter(mAdapter);    }    private void initFragment() {        for (int i = 0; i < 4; i++) {            list.add(new Fragment01());        }    }}
二,ChildFragment :方法里
public class ChildFragment extends Fragment {    int page = 0;    private PullToRefreshListView plv;    private MAdapter mAdapter;    String baseUrl = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0";    private List<News.DataBean> list = new ArrayList<News.DataBean>();    private ListView lv;    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        //加载布局        View view = inflater.inflate(R.layout.item0, null);       plv = (PullToRefreshListView) view.findViewById(R.id.plv);        //添加头部布局        lv = plv.getRefreshableView();        return view;    }    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        getNetData(page);//获取list集合的方法;        //主线程继续往下走,和刚才开启的线程互不影响;0.0001        //添加适配器        mAdapter = new MAdapter();        lv.setAdapter(mAdapter);        plv.setMode(PullToRefreshBase.Mode.BOTH);//上下拉都支持        //设置监听        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {                list.clear();                getNetData(0);            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {                page++;                getNetData(20);            }        });    }    private void getNetData(int page) {        new MAsync().execute(baseUrl + page);    }    //设置适配器    class MAdapter extends BaseAdapter {        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int i) {            return list.get(i);        }        @Override        public long getItemId(int i) {            return i;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            view = View.inflate(getActivity(), R.layout.listview, null);            ImageView imageview = (ImageView) view.findViewById(R.id.image);            TextView tv = (TextView) view.findViewById(R.id.tv);            tv.setText(list.get(i).getTITLE());            ImageLoader.getInstance().displayImage((String) list.get(i).getIMAGEURL(), imageview);            return view;        }    }    class MAsync extends AsyncTask<String, Void, String> {        @Override        protected String doInBackground(String... strings) {            return NetWordUtils.getNetjson(strings[0]);        }        @Override        protected void onPostExecute(String s) {            super.onPostExecute(s);            Gson gson = new Gson();            News news = gson.fromJson(s, News.class);            List<News.DataBean> data = news.getData();            list.addAll(data);            mAdapter.notifyDataSetChanged();            plv.onRefreshComplete();        }    }    private class BannerLoader implements ImageLoaderInterface {        @Override        public void displayImage(Context context, Object path, View imageView) {            ImageLoader instance = ImageLoader.getInstance();            instance.displayImage((String) path, (ImageView) imageView);        }        @Override        public View createImageView(Context context) {            return null;        }    }}
三,Fragment01 :方法里
public class Fragment01 extends Fragment {    private TabLayout tabLayout;    private ViewPager viewpager;    ArrayList<Fragment> list = new ArrayList<Fragment>();    String[] temp = {"推荐", "课程", "实战", "职业路径"};    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment01, null);        tabLayout = (TabLayout) view.findViewById(R.id.tl_titles);        viewpager = (ViewPager) view.findViewById(R.id.viewpager);        return view;    }    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        indata();        MYaptera mYapter = new MYaptera(getChildFragmentManager());        tabLayout.setTabMode(TabLayout.MODE_FIXED);        tabLayout.setupWithViewPager(viewpager);        viewpager.setAdapter(mYapter);    }    private void indata() {        for (int i = 0; i < 4; i++) {            list.add(new ChildFragment());        }    }    class MYaptera extends FragmentPagerAdapter {        public MYaptera(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return list.get(position);        }        @Override        public int getCount() {            return list.size();        }        @Override        public CharSequence getPageTitle(int position) {            return temp[position];        }    }}
四,Fragment02,Fragment03,Fragment04 :方法里,只需要改03,04
public class Fragment02 extends Fragment {    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment02, null);        return view;    }}
五,MApp :方法里
public class MApp extends Application {    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration imageLoaderConfiguration=new ImageLoaderConfiguration.Builder(this).build();        ImageLoader.getInstance().init(imageLoaderConfiguration);    }}
六,NetWordUtils :方法里
public class NetWordUtils {    private static String tag = "NetWordUtils";    /**     * 获取网络json     *     * @param urlString     * @return     */    public static String getNetjson(String urlString) {        try {            URL url = new URL(urlString);            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();            urlConnection.setRequestMethod("GET");//若果是get请求可以不用配置; 其他请求必须配置            urlConnection.setConnectTimeout(8000);//设置链接超时间            InputStream inputStream = urlConnection.getInputStream();//获取网络返回的输入流;            //可拼接的字符串            StringBuilder stringBuilder = new StringBuilder();            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));            String temp = "";            while ((temp = bufferedReader.readLine()) != null) {                stringBuilder.append(temp);                temp = "";            }            //这个是网络获取的数据            String data = stringBuilder.toString();            Log.e(tag, "getData: " + data);            return data;        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return "";    }    /**     * 获取网络图片的工具类     *     * @param urlString     * @return     */    public static Bitmap getNetBitmap(String urlString) {        try {            //用URL封装链接地址;            URL url = new URL(urlString);            //用url打开链接            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();            //联网的状态码            int responseCode = urlConnection.getResponseCode();            if (responseCode == 200) {                //链接上获取输入流                InputStream inputStream = urlConnection.getInputStream();                //把流直接转换成bitmap(系统提供的BitmapFactory)                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//BitmapFactory是个工具类,系统提供的                return bitmap;            } else {                Log.e(tag, "网络状态码:: " + responseCode);            }        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }}
七,News :方法里 
   接口要解析的内容,但是注意 ChildFragment 要需要改改

阅读全文
0 0
原创粉丝点击