Android基础篇-Android底部导航Tabbar -[ViewPager+RadioGroup+RadioButton+Fragment]实现

先看看效果图吧【可手势滑动】

在这里插入图片描述
在这里插入图片描述
上面是一种样式,对于中间是个很大的按钮的,就换一个风格,仅提供参考
在这里插入图片描述

第一种风格的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/mainBackground1"
    android:orientation="vertical">

    <com.wzq.light_or_night.widget.NoScrollViewPager
        android:id="@+id/vp_Main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-12dp"
        android:layout_weight="1"
        android:background="?attr/mainBackground1"/>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/bg_main_bottom"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="@dimen/dp_5"
        android:paddingBottom="@dimen/dp_5">

        <RadioButton
            android:id="@+id/rb_todo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_weight="1"
            android:background="@color/translate"
            android:button="@null"
            android:drawableTop="@drawable/db_todo_bg"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:text="xxx"
            android:checked="true"
            android:textColor="@drawable/db_main_text_color"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_model"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_weight="1"
            android:background="@color/translate"
            android:button="@null"
            android:drawableTop="@drawable/db_model_bg"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:text="xxx"
            android:textColor="@drawable/db_main_text_color"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_electrification"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_1"
            android:layout_weight="1"
            android:background="@color/translate"
            android:button="@null"
            android:drawableTop="@drawable/db_electrification_bg"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:text="xxxxx"
            android:textColor="@drawable/db_main_text_color"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_consulting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_weight="1"
            android:background="@color/translate"
            android:button="@null"
            android:drawableTop="@drawable/db_consulting_bg"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:text="xxx"
            android:textColor="@drawable/db_main_text_color"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_mine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_weight="1"
            android:background="@color/translate"
            android:button="@null"
            android:drawableTop="@drawable/db_mine_bg"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:text="xxx"
            android:textColor="@drawable/db_main_text_color"
            android:textSize="10sp" />
    </RadioGroup>


</LinearLayout>

第二种风格的布局代码

<?xml version="1.0" encoding="utf-8"?>
<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=".style3.Style3Activity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tabs_rg" />

    <RadioGroup
        android:id="@+id/tabs_rg"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="#dcdcdc"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/today_tab"
            style="@style/Custom.TabRadioButton"
            android:checked="true"
            android:drawableTop="@drawable/db_todo_bg"
            android:text="今日" />

        <RadioButton
            android:id="@+id/record_tab"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/db_model_bg"
            android:text="记录" />

        <View style="@style/Custom.TabRadioButton"
            />

        <RadioButton
            android:id="@+id/contact_tab"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/db_todo_bg"
            android:text="通讯录" />

        <RadioButton
            android:id="@+id/settings_tab"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/db_mine_bg"
            android:text="设置" />
    </RadioGroup>

    <ImageView
        android:id="@+id/sign_iv"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:src="@drawable/default_head"/>
</RelativeLayout>

NoScrollViewPager代码:

package com.wzq.light_or_night.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class NoScrollViewPager extends ViewPager {
    private boolean isScroll;
    public NoScrollViewPager(Context context, AttributeSet attrs){
        super(context, attrs);
    }
    public NoScrollViewPager(Context context) {
        super(context);
    }
    /**
     * 1.dispatchTouchEvent一般情况不做处理
     *,如果修改了默认的返回值,子孩子都无法收到事件
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);   // return true;不行
    }
    /**
     * 是否拦截
     * 拦截:会走到自己的onTouchEvent方法里面来
     * 不拦截:事件传递给子孩子
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // return false;//可行,不拦截事件,
        // return true;//不行,孩子无法处理事件
        //return super.onInterceptTouchEvent(ev);//不行,会有细微移动
        if (isScroll){
            return super.onInterceptTouchEvent(ev);
        }else{
            return false;
        }
    }
    /**
     * 是否消费事件
     * 消费:事件就结束
     * 不消费:往父控件传
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //return false;// 可行,不消费,传给父控件
        //return true;// 可行,消费,拦截事件
        //super.onTouchEvent(ev); //不行,
        //虽然onInterceptTouchEvent中拦截了,
        //但是如果viewpage里面子控件不是viewgroup,还是会调用这个方法.
        if (isScroll){
            return super.onTouchEvent(ev);
        }else {
            return true;// 可行,消费,拦截事件
        }
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {

        super.setCurrentItem(item, smoothScroll);
    }
    @Override
    public void setCurrentItem(int item) {

        super.setCurrentItem(item, false);
    }
    public void setScroll(boolean scroll) {
        isScroll = scroll;
    }
}

drawble里部分代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"     //点击选择是的样式
        android:drawable="@drawable/select_electrification" />
    <item
        android:state_pressed="true"   //按压时候的样式
        android:drawable="@drawable/select_electrification"/>
    <item android:drawable="@drawable/unselect_electrification"/>
</selector>
····
其他功能模块类似这么写

MainActivity代码:

  public class MainActivity extends BaseActivity {
  private NoScrollViewPager vpMain;
    private RadioButton rbTodo;
    private RadioButton rbModel;
    private RadioButton rbElectrification;
    private RadioButton rbConsulting;
    private RadioButton rbMine;
    private CheckBox mineLight;
    private DrawerLayout drawerLayout1;
    private int index = -1;
    private List<BaseFragment> fragments;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private Fragment5 fragment5;



    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
//        ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, 				R.style.AppTheme, R.style.NightAppTheme);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        vpMain = (NoScrollViewPager) findViewById(R.id.vp_Main);
        rbTodo = (RadioButton) findViewById(R.id.rb_todo);
        rbModel = (RadioButton) findViewById(R.id.rb_model);
        rbElectrification = (RadioButton) findViewById(R.id.rb_electrification);
        rbConsulting = (RadioButton) findViewById(R.id.rb_consulting);
        rbMine = (RadioButton) findViewById(R.id.rb_mine);
    //初始化fragment模块
        initFragments();
        MyPageFragmentAdapter pageFragmentAdapter = new       MyPageFragmentAdapter(getSupportFragmentManager(), fragments);
        vpMain.setAdapter(pageFragmentAdapter);
        vpMain.setOffscreenPageLimit(5);
        vpMain.setScroll(false);//手势滑动切换功能模块
        vpMain.addOnPageChangeListener(new MyOnPageListener() {
            @Override
            public void onMyPageSelected(int position) {
                switch (position) {
                    case 0:
                        rbTodo.setChecked(true);
                        break;
                    case 1:
                        rbModel.setChecked(true);
                        break;
                    case 2:
                        rbElectrification.setChecked(true);
                        break;
                    case 3:
                        rbConsulting.setChecked(true);
                        break;
                    case 4:
                        rbMine.setChecked(true);
                        break;
                }
            }
        });
        changeCurrentViewPage(rbTodo, 0);
        changeCurrentViewPage(rbModel, 1);
        changeCurrentViewPage(rbElectrification, 2);
        changeCurrentViewPage(rbConsulting, 3);
        changeCurrentViewPage(rbMine, 4);

    }
    private void initFragments() {
        if (fragment1 == null) {
            fragment1 = new Fragment1();
        }
        if (fragment2 == null) {
            fragment2 = new Fragment2();
        }
        if (fragment3 == null) {
            fragment3 = new Fragment3();
        }
        if (fragment4 == null) {
            fragment4 = new Fragment4();
        }
        if (fragment5 == null) {
            fragment5 = new Fragment5();
        }

        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        fragments.add(fragment4);
        fragments.add(fragment5);
    }


    private void changeCurrentViewPage(RadioButton rb, final int pos) {
        rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    vpMain.setCurrentItem(pos);
                    index = pos;
                }
            }
        });
    }

以上是部分代码,理解的同学就可以自己写了

当然我会贴上我的github 项目地址:https://github.com/KuoLuoC/AndroidDemo1
这个demo东西不止这个,还有抽屉效果、夜间模式等等,希望能帮助到您!觉得不错就给点个赞和star吧!!!!感谢

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过自定义底部 Tabbar实现凸起透明底部的效果。下面是一种实现方式: 1. 在 `uniapp` 项目的 `static` 目录下创建一个 `custom-tabbar` 文件夹,用于存放自定义 Tabbar 的相关资源。 2. 在 `custom-tabbar` 文件夹中创建一个 `tabbar.vue` 组件文件,作为自定义 Tabbar 的模板。在模板中,你可以使用 `<template>` 标签定义底部 Tabbar 的布局。 3. 在 `tabbar.vue` 组件中,使用 `<slot>` 标签创建插槽,用于显示外部传入的 Tabbar 内容。同时,在该标签上添加样式,使其能够凸起显示。 ```html <template> <div class="custom-tabbar"> <slot></slot> </div> </template> <style scoped> .custom-tabbar { position: fixed; bottom: 0; left: 0; right: 0; background-color: transparent; border-top: 1px solid #ccc; display: flex; flex-direction: row; } </style> ``` 4. 在需要使用自定义 Tabbar 的页面中引入 `custom-tabbar` 组件,并在该组件内部插入具体的 Tabbar 内容。 ```html <template> <div> <!-- 此处为页面内容 --> ... <!-- 引入自定义 Tabbar --> <custom-tabbar> <!-- 插入具体的 Tabbar 内容 --> <view class="tabbar-item">首页</view> <view class="tabbar-item">消息</view> <view class="tabbar-item">我的</view> </custom-tabbar> </div> </template> <style> .tabbar-item { flex: 1; padding: 10px; text-align: center; } </style> ``` 5. 根据实际需求,你可以在 Tabbar 的模板中添加更多的样式和交互逻辑。 这样,你就可以实现一个自定义的底部 Tabbar,使其能够凸起并具有透明底部的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值