码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment中TabLayout加FragmentViewPager

时间:2017-10-25 19:52:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:and   instance   tab   sel   center   void   viewgroup   selected   override   

这是要放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];
    }
}

 

Fragment中TabLayout加FragmentViewPager

标签:and   instance   tab   sel   center   void   viewgroup   selected   override   

原文地址:http://www.cnblogs.com/cainiaodongdong/p/7731586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!