zoukankan      html  css  js  c++  java
  • 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];
        }
    }
     
     
  • 相关阅读:
    C指针
    redis五种基本数据类型
    mongoDB MapReduce
    JSON传参
    mongodb查询实练
    逻辑数据结构和存储数据结构
    线性表的顺序存储和链式存储
    数据结构和算法关系
    UIActivityIndicatorView使用
    Swift中格式化日期
  • 原文地址:https://www.cnblogs.com/98k98k/p/7942729.html
Copyright © 2011-2022 走看看