TabLayout

来源:互联网 发布:俄罗斯人口分布知乎 编辑:程序博客网 时间:2024/06/07 11:23
//主布局
<?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/draw"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.example.nm.myapplication.MainActivity">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            >           <FrameLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:id="@+id/frag"                android:layout_weight="1"                ></FrameLayout>            <RadioGroup                android:layout_width="match_parent"                android:layout_height="match_parent"                android:id="@+id/group"                android:layout_weight="9"                android:orientation="horizontal"                android:gravity="center"                >                <RadioButton                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:id="@+id/radio1"                    android:button="@null"                    android:text="首页"                    android:gravity="center"                    />                <RadioButton                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:id="@+id/radio2"                    android:button="@null"                    android:gravity="center"                    android:text="想法"                    />                <RadioButton                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:id="@+id/radio3"                    android:button="@null"                    android:gravity="center"                    android:text="我的"                    />            </RadioGroup>        </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/line"        android:background="#f00"        android:layout_gravity="left"        android:orientation="vertical"        >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/ic_launcher"            />        <ListView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/list"            ></ListView>    </LinearLayout></android.support.v4.widget.DrawerLayout>
_______________________________________________________________________________
//f1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.design.widget.TabLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tab"        app:tabGravity="center"        app:tabIndicatorColor="@color/colorAccent"        app:tabMode="scrollable"        app:tabSelectedTextColor="@color/colorPrimaryDark"        app:tabTextColor="@color/colorPrimary"        ></android.support.design.widget.TabLayout>    <android.support.v4.view.ViewPager        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/fvp"        ></android.support.v4.view.ViewPager>  </LinearLayout>
_________________________________________________________________________________________
//MainActivity
package com.example.nm.myapplication;import android.graphics.Color;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.FrameLayout;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.RadioButton;import android.widget.RadioGroup;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    FrameLayout fragment;    DrawerLayout draw;    LinearLayout line;    ListView list;    ArrayList<String> lists=new ArrayList<>();    RadioButton radio1,radio2,radio3;    F1 f1;    F2 f2;    F3 f3;    F5 f5;    F6 f6;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        draw=(DrawerLayout) findViewById(R.id.draw);        line=(LinearLayout) findViewById(R.id.line);        list=(ListView) findViewById(R.id.list);        fragment=(FrameLayout) findViewById(R.id.frag);        radio1=(RadioButton) findViewById(R.id.radio1);        radio2=(RadioButton) findViewById(R.id.radio2);        radio3=(RadioButton) findViewById(R.id.radio3);        lists.add("个人信息");        lists.add("资料");        lists.add("关于");        getSupportFragmentManager().beginTransaction().replace(R.id.frag,new F1()).commit();        list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,lists));        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                draw.closeDrawer(line);            }        });        radio1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getSupportFragmentManager().beginTransaction().replace(R.id.frag,new F1()).commit();            }        });        radio2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getSupportFragmentManager().beginTransaction().replace(R.id.frag,new F5()).commit();            }        });        radio3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getSupportFragmentManager().beginTransaction().replace(R.id.frag,new F6()).commit();            }        });    }}

__________________________________________________________________________________________________________________________________-
//F1
package com.example.nm.myapplication;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import java.util.ArrayList;import java.util.List;/** * Created by 爱新觉罗璎汉 on 2017/11/17. */public class F1 extends Fragment {    TabLayout tab;    ViewPager pager;    List<String> list=new ArrayList<>();    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view=inflater.inflate(R.layout.f1,container,false);        tab=(TabLayout) view.findViewById(R.id.tab);        pager=(ViewPager) view.findViewById(R.id.fvp);        //list.clear();        list.add("动态");        list.add("热门");        list.add("发现");        pager.setAdapter(new FAdapter(getChildFragmentManager(), list));        tab.setupWithViewPager(pager);        return view;    }}

______________________________________________________________________________________
F2
package com.example.nm.myapplication;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.lang.reflect.Array;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;/** * Created by 爱新觉罗璎汉 on 2017/11/17. */public class F2 extends Fragment {PullToRefreshListView pull;    int num=1,type=1;    Adapter adapter;    String url="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num;    ArrayList<bean.DataBean> list=new ArrayList<>();    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view=inflater.inflate(R.layout.f2,container,false);        pull=(PullToRefreshListView)view.findViewById(R.id.pull);        pull.setMode(PullToRefreshBase.Mode.BOTH);        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {                num=1;                type=1;                url="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num;                new anys().execute(url);            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {                num++;                type=2;                url="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num;                new anys().execute(url);            }        });        //        new anys().execute(url);        return view;    }    class anys extends AsyncTask<String,Void,String>{        @Override        protected String doInBackground(String... params) {            StringBuilder builder=null;            try {                URL url = new URL(params[0]);                HttpURLConnection con = (HttpURLConnection)url.openConnection();                con.setRequestMethod("GET");                con.setReadTimeout(5000);                con.setConnectTimeout(5000);                if(con.getResponseCode()==200){                    InputStream stream = con.getInputStream();                    InputStreamReader reader = new InputStreamReader(stream);                    BufferedReader buf = new BufferedReader(reader);                    String s=null;                    builder = new StringBuilder();                    while((s=buf.readLine())!=null){                        builder.append(s);                    }                }            } catch (Exception e) {                e.printStackTrace();            }            return builder.toString();        }        @Override        protected void onPostExecute(String s) {            Gson gson = new Gson();            bean bean = gson.fromJson(s, bean.class);            if(type==1){                list.clear();                list.addAll(bean.getData());            }else{                list.addAll(bean.getData());            }            Adapter();            new Handler().postDelayed(new Runnable() {                @Override                public void run() {                    pull.onRefreshComplete();                }            },500);        }    }    public void Adapter(){        if(adapter==null){            pull.setAdapter(new Adapter(getActivity(),list));        }else{            adapter.notifyDataSetChanged();        }    }}

_________________________________________________________________________________________________________________
//TabLayout适配器
package com.example.nm.myapplication;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.ArrayList;import java.util.List;/** * Created by 爱新觉罗璎汉 on 2017/11/17. */public class FAdapter extends FragmentPagerAdapter {    List<String> list;    public FAdapter(FragmentManager fm, List<String> list) {        super(fm);        this.list = list;    }    @Override    public Fragment getItem(int position) {      if(position==0){            return new F2();        }else if(position==1){            return new F3();        }else{            return new F4();        }    }    @Override    public int getCount() {        return list.size();    }    @Override    public CharSequence getPageTitle(int position) {        return list.get(position);    }}