TabLayout

来源:互联网 发布:问卷网数据导出wps 编辑:程序博客网 时间:2024/06/17 21:24
依赖:吐舌头
compile 'com.android.support:design:24.2.0'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.bwie.tablayoutdemo.MainActivity">    <android.support.design.widget.TabLayout        android:id="@+id/tabs"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:tabIndicatorColor="@color/red"        app:tabMode="scrollable"        app:tabSelectedTextColor="@color/red"        app:tabTextColor="@color/black"/>    <android.support.v4.view.ViewPager        android:id="@+id/vp_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>

MainActivity.java:
public class MainActivity extends AppCompatActivity {    private TabLayout mTabLayout;    private ViewPager mViewPager;    private String[] channels = {"推荐","热点","体育","娱乐","社会","汽车","教育","财经","科技","游戏"};    private String[] urlS = {            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1",            "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1"    };    private LayoutInflater mInflater;    private List<String> mTitleList = new ArrayList<>();//页卡标题集合    private List<ChannelFragment> mViewList = new ArrayList<>();//页卡视图集合    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mViewPager = (ViewPager) findViewById(R.id.vp_view);        mTabLayout = (TabLayout) findViewById(R.id.tabs);        mInflater = LayoutInflater.from(this);        for(int i=0;i<channels.length;i++){            //创建栏目的fragment            ChannelFragment fragment = new ChannelFragment();            Bundle b = new Bundle();            b.putString("name", channels[i]);//传递名字            b.putString("url", urlS[i]);            fragment.setArguments(b);            //收集fragment            mViewList.add(fragment);            //给tablayout添加tab选项卡            mTabLayout.addTab(mTabLayout.newTab().setText(channels[i]));//添加tab选项卡        }        FragmentManager fm = getSupportFragmentManager();        MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(fm, mViewList);        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器    }    class MyFragmentPagerAdapter extends FragmentPagerAdapter{        private List<ChannelFragment> mViewList;        public MyFragmentPagerAdapter(FragmentManager fm, List<ChannelFragment> mViewList) {            super(fm);            this.mViewList = mViewList;        }        @Override        public Fragment getItem(int position) {            return mViewList.get(position);        }        @Override        public int getCount() {            return mViewList.size();        }        @Override        public CharSequence getPageTitle(int position) {            return channels[position];//页卡标题        }    }}

ChannelFragment:
public class ChannelFragment extends Fragment {    private String name;    private String news_url;    private TextView textView;    private ListView listView;    private List<User.NewslistBean> newslist;    private LvAdapter adapter;    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Bundle bundle = getArguments();        name = (String) bundle.get("name");        news_url = (String) bundle.get("url");    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.pager_item, null);        listView=(ListView) view.findViewById(R.id.list_item);        //通过url进行网络请求,解析json,多条目展示在ListView中        new AsyncTask<String,Integer,String>(){            @Override            protected String doInBackground(String... strings) {                String str=new NetWorksUtils().getJsonUrlJiexi(news_url+"1");                return str;            }            @Override            protected void onPostExecute(String s) {                super.onPostExecute(s);                Gson gson = new Gson();                User user = gson.fromJson(s, User.class);                newslist = user.getNewslist();                adapter=new LvAdapter(newslist,getActivity());                listView.setAdapter(adapter);            }        }.execute(news_url);        return view;    }}
fragment.xml:
<?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">    <ListView        android:id="@+id/list_item"        android:layout_width="match_parent"        android:layout_height="wrap_content"></ListView></LinearLayout>

LvAdapter适配器
public class LvAdapter extends BaseAdapter {    private List<User.NewslistBean> list;    private Context context;    public LvAdapter(List<User.NewslistBean> list, Context context) {        this.list = list;        this.context = context;    }    @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(context,R.layout.item,null);        TextView textView=(TextView) view.findViewById(R.id.text);        textView.setText(list.get(i).getTitle());        return view;    }}

item:xml:
<?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:id="@+id/text"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

Color:
<?xml version="1.0" encoding="utf-8"?><resources>    <color name="colorPrimary">#3F51B5</color>    <color name="colorPrimaryDark">#303F9F</color>    <color name="colorAccent">#FF4081</color>    <color name="red">#F00</color>    <color name="black">#000</color></resources>