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

Android底部导航Tabbar ViewPager+RadioGroup+RadioButton+Fragment

  • 先看看效果图吧【可手势滑动】
    • 第一种风格的布局代码
    • 第二种风格的布局代码
    • NoScrollViewPager代码:
    • drawble里部分代码:
    • MainActivity代码:
    • 以上是部分代码,理解的同学就可以自己写了

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

Android基础篇-Android底部导航Tabbar -[ViewPager+RadioGroup+RadioButton+Fragment]实现_第1张图片
Android基础篇-Android底部导航Tabbar -[ViewPager+RadioGroup+RadioButton+Fragment]实现_第2张图片
上面是一种样式,对于中间是个很大的按钮的,就换一个风格,仅提供参考
Android基础篇-Android底部导航Tabbar -[ViewPager+RadioGroup+RadioButton+Fragment]实现_第3张图片

第一种风格的布局代码




    

    

        

        

        

        

        
    



第二种风格的布局代码




    

    

        

        

        

        

        
    

    

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里部分代码:



    
    
    

····
其他功能模块类似这么写

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 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吧!!!!感谢

你可能感兴趣的:(Android自定义View,Android四大组件,Android导航)