TabLayout和ViewPager的简单使用

来源:互联网 发布:重生之一叶而知秋书包 编辑:程序博客网 时间:2024/06/04 17:50
package com.fragment;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.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.lenovo.kuankanmanhua.R;import java.util.ArrayList;import java.util.List;public class Remen extends Fragment {    private TabLayout tabLayout;   //定义TabLayout    private ViewPager view_vp;     //定义viewPager    private List<Fragment> list_fragment = new ArrayList<>();                                //定义要装fragment的列表    private List<String> list_title = new ArrayList<>();                                     //tab名称列表    private Zhouy1 zhouy1;    private Zhouy2 zhouy2;    private Zhouy3 zhouy3;    private Zhouy4 zhouy4;    private Zhouy5 zhouy5;    private Zhouy6 zhouy6;    private Zhouy7 zhouy7;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.remen, null);        initview(view);        return view;    }    private void initview(View view) {        tabLayout = (TabLayout) view.findViewById(R.id.tab_FindFragment_title);        view_vp = (ViewPager) view.findViewById(R.id.vp_FindFragment_pager);        //初始化各fragment        zhouy1 = new Zhouy1();        zhouy2 = new Zhouy2();        zhouy3 = new Zhouy3();        zhouy4 = new Zhouy4();        zhouy5 = new Zhouy5();        zhouy6 = new Zhouy6();        zhouy7 = new Zhouy7();        //将fragment装进列表中        list_fragment.add(zhouy1);        list_fragment.add(zhouy2);        list_fragment.add(zhouy3);        list_fragment.add(zhouy4);        list_fragment.add(zhouy5);        list_fragment.add(zhouy6);        list_fragment.add(zhouy7);        //将名称加载tab名字列表        list_title.add("周一");        list_title.add("周二");        list_title.add("周三");        list_title.add("周四");        list_title.add("周五");        list_title.add("周六");        list_title.add("周日");        //设置TabLayout的模式        //tab_title.setTabMode(TabLayout.MODE_FIXED);        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);        //为TabLayout添加tab名称        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(3)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(4)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(5)));        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(6)));        view_vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {            @Override            public Fragment getItem(int position) {                return list_fragment.get(position);            }            @Override            public int getCount() {                return list_title.size();            }            //此方法用来显示tab上的名字            @Override            public CharSequence getPageTitle(int position) {                return list_title.get(position%list_title.size());            }        });        //TabLayout加载viewpager        tabLayout.setupWithViewPager(view_vp);    }}
//布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical">    <android.support.design.widget.TabLayout        android:id="@+id/tab_FindFragment_title"        android:layout_width="match_parent"        android:layout_height="40dp"        app:tabIndicatorColor="#f7c203"        app:tabSelectedTextColor="#af976262"        app:tabTextColor="#dc0b0707"        />    <android.support.v4.view.ViewPager        android:id="@+id/vp_FindFragment_pager"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1"        /></LinearLayout>

原创粉丝点击