Fragment中TabLayout加FragmentViewPager
Posted 菜鸟东东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fragment中TabLayout加FragmentViewPager相关的知识,希望对你有一定的参考价值。
这是要放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的主要内容,如果未能解决你的问题,请参考以下文章
cathome 猫家 开发日记-tablayout+viewpager+fragment
安卓中viewpager+tablayout+fragment懒加载怎么做
Here Maps - 在 TabLayout 的 Fragment 中使用 MapFragment
如何使用 TabLayout 在 Fragment 中使用 CardView 行实现 RecyclerView