6、底部tabBar第三方按钮

最火开源框架MVVMhabit
一. 准备工作
二. 快速上手

三. 数据绑定

  1. TextView绑定
  2. onClick绑定
  3. ImageView绑定
  4. ListView绑定
  5. RecyclerView绑定

四. 其他

  1. toolbar头部标题
  2. 底部tabBar第三方按钮

1、引用库

//底部tabBar
    implementation('me.majiajie:pager-bottom-tab-strip:2.2.5') {
        exclude group: 'com.android.support'
    }

2、xml 设置标签 me.majiajie.pagerbottomtabstrip.PageBottomTabLayout
fragment也要用到FrameLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
<!--fragment-->
    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F0F0F0" />
<!--tabBar-->
    <me.majiajie.pagerbottomtabstrip.PageBottomTabLayout
        android:id="@+id/pager_bottom_tab"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:elevation="8dp" />
</LinearLayout>

3、activity 初始化framgent相关代码,在onCreate中调用initData就行了

四个Fragment页面这里就不详细写了,创建类继承Fragment就行了

//其他代码...

private List<Fragment> mFragments;
private List<Fragment> mFragmentDatas;
//fragment切换是否刷新
private Boolean isCheck = true;

//初始化方法
public void initData() {
    initFragment();
    initBottomTab();
}

private void initFragment() {
    mFragmentDatas = new ArrayList<>();
    mFragmentDatas.add(new TabBar1Fragment());
    mFragmentDatas.add(new TabBar2Fragment());
    mFragmentDatas.add(new TabBar3Fragment());
    mFragmentDatas.add(new TabBar4Fragment());

    mFragments = new ArrayList<>();
    for (int i = 0; i < mFragmentDatas.size(); i++) {
        mFragments.add(null);
    }

    //默认选中第一个
    addFragment(0);
}

private void initBottomTab() {
    //初始化tabBar控件
    NavigationController navigationController = binding.pagerBottomTab.material()
            .addItem(R.mipmap.yingyong, "应用")
            .addItem(R.mipmap.huanzhe, "工作")
            .addItem(R.mipmap.xiaoxi_select, "消息")
            .addItem(R.mipmap.wode_select, "我的")
            .setDefaultColor(ContextCompat.getColor(this, R.color.black))
            .setMessageNumberColor(ContextCompat.getColor(this, R.color.blue))
            .setMessageBackgroundColor(ContextCompat.getColor(this, R.color.green))
            .build();
    //底部按钮的点击事件监听
    navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {


        @Override
        public void onSelected(int index, int old) {
            addFragment(index);
        }

        @Override
        public void onRepeat(int index) {
        }
    });
}


private void addFragment(int index) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    //两种fragment加载方式,replace每次都初始化fragment;add只有第一次初始化
    if (isCheck) {
        mFragments.set(index, mFragmentDatas.get(index));
        transaction.replace(R.id.frameLayout, mFragments.get(index));
    } else {
        if (mFragments.get(index) == null) {
            mFragments.set(index, mFragmentDatas.get(index));
            transaction.add(R.id.frameLayout, mFragments.get(index));
        }
        hideFragment(transaction);
        transaction.show(mFragments.get(index));
    }
    transaction.commitAllowingStateLoss();
}

//隐藏所有的fragment
private void hideFragment(FragmentTransaction transaction) {
    for (int i = 0; i < mFragmentDatas.size(); i++) {
        if (mFragments.get(i) != null) {
            transaction.hide(mFragments.get(i));
        }
    }
}
//其他代码...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Java基础 什么是重载,什么是重写?有什么区别?重载(Overload):(1)Overloading是一个类中...
    勤息嘻嘻嘻阅读 701评论 0 1
  • Fragment要点 1、Fragment作为Activity界面的一部分组成出现 2、可以在一个Activity...
    玉圣阅读 1,324评论 0 16
  • 《Android Fragment完全解析,关于碎片你所需知道的一切》 我们都知道,Android上的界面展示都是...
    cxm11阅读 2,361评论 2 19
  • 前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本...
    斜杠时光阅读 2,643评论 4 22
  • 今天周一,这一天文畅还是比较听话,作业也完成的不错,就是坐姿还是要时刻提醒。继续努力。
    文畅妈妈阅读 132评论 0 0

友情链接更多精彩内容