>>分享Android开发相关的技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 21779 个阅读者 刷新本主题
 * 贴子主题:  Android中竖着的Tablayout的简单使用 回复文章 点赞(0)  收藏  
作者:admin    发表时间:2019-12-31 21:58:43     消息  查看  搜索  好友  复制  引用

    

视图效果

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小
db5104bce737ea0c63a34106cf1c197.png

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">
</q.rorbin.verticaltablayout.VerticalTabLayout>
<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">
</android.support.v4.view.ViewPager>
</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);
    }
}

原文链接:https://www.jianshu.com/p/f09d6775f84a


程序猿的技术大观园:www.javathinker.net

[这个贴子最后由 admin 在 2020-01-01 16:12:01 重新编辑]
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=1116

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员