美文网首页安卓开发相关
Android中竖着的Tablayout的简单使用

Android中竖着的Tablayout的简单使用

作者: Topone | 来源:发表于2020-12-22 07:40 被阅读0次

    1.添加依赖

    implementation 'q.rorbin:VerticalTabLayout:1.2.5'
    

    2.XML布局的使用

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.douwenxuan.view.category.CategoryFragment">
     
    <q.rorbin.verticaltablayout.VerticalTabLayout
        android:id="@+id/vTab"
        android:layout_alignParentLeft="true"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:background="@color/white"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_sort"
        android:layout_toRightOf="@id/vTab"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
     
    </RelativeLayout>
    

    3.Java中的书写

    public class CategoryFragment extends BaseFragment implements CategoryContract.View {
     
        @BindView(R.id.vTab)
        VerticalTabLayout vTab;
        @BindView(R.id.vp_sort)
        ViewPager vpSort;
        Unbinder unbinder;
     
     
        @Override
        protected int getLayout() {
            return R.layout.fragment_category;
        }
     
        @Override
        protected void initView(View view) {
     
        }
     
        @Override
        protected void initData() {
            ((CategoryPresenter)presenter).category();
        }
     
        @Override
        protected IPresenter createPresenter() {
            return new CategoryPresenter();
        }
     
        @Override
        public void showErrMsg(String err) {
            Toast.makeText(getActivity(), err, Toast.LENGTH_SHORT).show();
        }
     
        @Override
        public void categoryDataReturn(List<CategoryListBean> categoryList) {
            final ArrayList<String> tabName = new ArrayList<>();
            final ArrayList<Fragment> fragments = new ArrayList<>();
     
            for (int i = 0; i < categoryList.size(); i++) {
                fragments.add(new CategoryTabFragment(categoryList.get(i).getName(), categoryList.get(i).getId() + ""));
                tabName.add(categoryList.get(i).getName());
            }
            FragTabAdapter fragTabAdapter = new FragTabAdapter(getChildFragmentManager(), fragments,
                    tabName);
            vpSort.setAdapter(fragTabAdapter);
            vTab.setupWithViewPager(vpSort);
        }
     
    }
    

    FragTabAdapter的书写

    public class FragTabAdapter extends FragmentPagerAdapter {
       private ArrayList<Fragment> fragments = new ArrayList<>();
       private ArrayList<String> tabName = new ArrayList<>();
    
       public FragTabAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String>
               tabName) {
           super(fm);
           this.fragments = fragments;
           this.tabName = tabName;
       }
    
       @Override
       public Fragment getItem(int i) {
           return fragments.get(i);
       }
    
       @Override
       public int getCount() {
           return fragments.size();
       }
    
       @Nullable
       @Override
       public CharSequence getPageTitle(int position) {
           return tabName.get(position);
       }
    }
    

    相关文章

      网友评论

        本文标题:Android中竖着的Tablayout的简单使用

        本文链接:https://www.haomeiwen.com/subject/iayjnktx.html