Fragment中TabLayout加FragmentViewPager

让tablayout中的Tab平铺:在tablayout布局中写:

app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"

 

这是要放TabLayout的Fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#FF00FF00"
        app:tabSelectedTextColor="#FF00FF00"
        app:tabTextColor="#FF000000"
        app:tabMode="scrollable"
        app:tabGravity="center"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

这是Fragment:
public class ZhuyeMyFragment extends Fragment{
    private String[] mTitle = new String[4];
    private String[] mData = new String[4];

    {
        for(int i=0;i<4;i++) {
            mTitle[i] = "title" + i;
            mData[i] = "data" + i;
        }
    }
    TabLayout mTabLayout ;
    ViewPager mViewPager ;
    FragmentPagerAdapter mAdapter;
    Zhuti zt;
    Zuixin zx;
    Zl zl;
    Rm rm;
    ArrayList<Fragment> flist;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.zhuye_layout,null);
        mTabLayout = (TabLayout)view.findViewById(R.id.tl);
        mViewPager = (ViewPager)view.findViewById(R.id.viewpager);
        FragmentManager man = getActivity().getSupportFragmentManager();
        initFragment();
        flist=new ArrayList<Fragment>();
        flist.add(zx);
        flist.add(zl);
        flist.add(rm);
        flist.add(zt);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        final TabLayout.TabLayoutOnPageChangeListener listener =
                new TabLayout.TabLayoutOnPageChangeListener(mTabLayout);
        mViewPager.addOnPageChangeListener(listener);
        mAdapter= new FragmentAdapter(man,flist);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setTabsFromPagerAdapter(mAdapter);
        return view;
    }

    private void initFragment() {
        zt=new Zhuti();
        zx=new Zuixin();
        zl=new Zl();
        rm=new Rm();
    }
}
这是FragmentAdapter:

public class FragmentAdapter extends FragmentPagerAdapter {

    private String [] title = {"最新日报","热门","专栏","主题日报"};
    private List<Fragment> fragmentList;
    public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }
    @Override
    public int getCount() {
        return fragmentList.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }
}
 
 

 

posted @ 2017-12-01 15:39  红领巾传递人  阅读(359)  评论(0编辑  收藏  举报