自定义底部导航栏 - BottomNavigation

  • 默认选中第一个子条目

使用

xml布局

<com.example.navigationlib.BottomNavigation
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:bottomNavigationBottomMargin="6dp"
        app:bottomNavigationDeliverLine="true"
        app:bottomNavigationDeliverLineColor="@color/gray"
        app:bottomNavigationDeliverLineWidth="1dp"
        app:bottomNavigationLeftMargin="27dp"
        app:bottomNavigationRightMargin="27dp"
        app:bottomNavigationTextColor="@color/bottom_navigation_text_color"
        app:bottomNavigationTextSize="10dp"
        app:bottomNavigationTopMargin="6dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
private BottomNavigation bottomNavigation;
private BaseFragment fragment ;

  
      //找控件
        bottomNavigation = (BottomNavigation) findViewById(R.id.bottomNavigation);

        /**
         * addItem() 添加子控件
         *
         *  @param int drawableId 图片
         *  @param  String title  标题
         */
        bottomNavigation.addItem(R.drawable.tab_recommend_selector,"推荐")
                .addItem(R.drawable.tab_special_selector, "专题")
                .addItem(R.drawable.tab_mine_selector, "我的")
                .apply();//提交

        //存放Fragment
        Object[] arr = new Object[3];
        arr[0] = MenuFragment.class;
        arr[1] = SpecialFragment.class;
        arr[2] = MyFrament.class;

        /**
         * setTabSelectedListener() 自控件选中监听
         *      onTabSelect() 选中
         *      onTabUnSelect() 未选中
         *      onTabReSelected() 重新选中
         */
        bottomNavigation.setTabSelectedListener(new BottomNavigation.OnTabSelectedListener() {
            @Override
            public void onTabSelect(View tab, int position) {
                /**
                 * MvpFragmentManager 添加Fragment的工具类
                 *       addOrShowFragment()  添加Fragment方法
                 *
                 * @param FragmentManager manager  布局管理器
                 * @param Class<? extends BaseFragment> willShowFragment  将要进入的Fragment
                 * @param BaseFragment preFragment 将要退出的Fragment(可以为null)
                 *  @param @IdRes int containerId  放置Fragment的容器FrameLayout
                 */
                fragment = (BaseFragment) MvpFragmentManager.addOrShowFragment(getSupportFragmentManager(), (Class<? extends BaseFragment>) arr[position], fragment, R.id.home_menu_fl);
            }

            @Override
            public void onTabUnSelect(View tab, int position) {
//                Toast.makeText(HomeActivity.this, "tab = "+tab+",position = "+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTabReSelected(View tab, int position) {
//                Toast.makeText(HomeActivity.this, "tab = "+tab+",position = "+position, Toast.LENGTH_SHORT).show();
            }
        });  

BottomNavigation类 ( 自定义底部导航栏 )

package com.example.navigationlib;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.DrawableRes;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;



import java.util.ArrayList;

public class BottomNavigation extends ConstraintLayout {

    private static final int HORIZONTAL_MARGIN_DP = 32;
    private static final int VERTICAL_MARGIN_DP = 12;

    private NavigationAdapter mAdapter;

    private ArrayList<Integer> mDrawableRes = new ArrayList<>();
    private ArrayList<String> mTitles = new ArrayList<>();
    private ColorStateList mTextColorStateList;
    private OnTabSelectedListener mTabSelectedListener;

    private Paint mLinePaint;

    private int mMarginLeft;
    private int mMarginBottom;
    private int mMarginRight;
    private int mMarginTop;
    private int mDrawableMargin;
    private int mTextSize;
    private int mDrawableIconWidth;
    private int mDrawableIconHeight;
    private int mDeliverLineWidth;
    private int mDeliverLineColor;

    private int mCurrentPosition = 0;


    private boolean mHasDeliverLine; // 是否显示分割线


    public BottomNavigation(Context context) {
        super(context);
    }

    public BottomNavigation(Context context, AttributeSet attrs) {
        super(context, attrs);
        initValue(attrs);
    }

    public BottomNavigation(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initValue(attrs);

    }


    private void initValue(AttributeSet set) {

        TypedArray array = getContext().obtainStyledAttributes(set, R.styleable.BottomNavigation);

        mMarginLeft = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationLeftMargin, dip2px(getContext(), HORIZONTAL_MARGIN_DP));
        mMarginRight = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationRightMargin, dip2px(getContext(), HORIZONTAL_MARGIN_DP));
        mMarginTop = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationTopMargin, dip2px(getContext(), VERTICAL_MARGIN_DP));
        mMarginBottom = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationBottomMargin, dip2px(getContext(), VERTICAL_MARGIN_DP));
        mDrawableIconWidth = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationDrawableWidth, 0);
        mDrawableIconHeight = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationDrawableHeight, 0);

        mDrawableMargin = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationDrawableMargin, 0);
        mTextSize = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationTextSize, 0);

        mTextColorStateList = array.getColorStateList(R.styleable.BottomNavigation_bottomNavigationTextColor);

        mHasDeliverLine = array.getBoolean(R.styleable.BottomNavigation_bottomNavigationDeliverLine, true);
        mDeliverLineWidth = array.getDimensionPixelSize(R.styleable.BottomNavigation_bottomNavigationDeliverLineWidth, dip2px(getContext(), 1));
        mDeliverLineColor = array.getColor(R.styleable.BottomNavigation_bottomNavigationDeliverLineColor, Color.GRAY);

        array.recycle();


        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(mDeliverLineColor);
        mLinePaint.setStrokeWidth(mDeliverLineWidth);

        setWillNotDraw(!mHasDeliverLine);
    }

    public void setTabSelectedListener(OnTabSelectedListener tabSelectedListener) {
        this.mTabSelectedListener = tabSelectedListener;
        if (mAdapter != null) {
            View tabView = mAdapter.getHolderByPosition(mCurrentPosition).mItemView;
            mTabSelectedListener.onTabSelect(tabView, (Integer) tabView.getTag());

        }
    }

    public BottomNavigation addItem(@DrawableRes int drawableId, String title) {

        mDrawableRes.add(drawableId);
        mTitles.add(title);
        return this;
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();


    }

    public BottomNavigation setCurrentPosition(int position) {

        mCurrentPosition = position;
        return this;
    }

    public void apply() {

        // 把这些drawable 和 title  应用到 我的布局上
        if (mDrawableRes.size() > 0) {
            ArrayList<TabData> list = new ArrayList<>();
            for (int i = 0; i < mDrawableRes.size(); i++) {
                list.add(new TabData(mDrawableRes.get(i), mTitles.get(i)));

            }
            apply(new SimpleNavigationAdapter(list));
        }

    }

    public void apply(NavigationAdapter<? extends TabHolder> adapter) {
        mAdapter = adapter;
        initView();

    }


    private void initView() {

        removeAllViews();


        int minTabWidth = Integer.MAX_VALUE; // 所有tab 中最小的那一个的宽度
        int maxTabHeight = 0; // 所有tab 中最高哪一个的高度
        int maxTabHeightIndex = 0; // 最高tab 的index

        for (int i = 0; i < mAdapter.getCount(); i++) {
            TabHolder holder = mAdapter.createHolder(this, i);
            addView(holder.mItemView);

            mAdapter.bindData(holder, i);


            // 计算每一个tab 的宽和高
            holder.mItemView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

            if (holder.mItemView.getMeasuredWidth() < minTabWidth) {
                minTabWidth = holder.mItemView.getMeasuredWidth();
            }


            if (holder.mItemView.getMeasuredHeight() > maxTabHeight) {
                maxTabHeight = holder.mItemView.getMeasuredHeight();
                maxTabHeightIndex = i;
            }


        }


        // 添加约束条件
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(this);

        int previousId = 0; // 用于记录上一个 id
        View view;
        int ids[] = new int[mAdapter.getCount()];

        // 在使用链的时候,如果是水平的链,那么就只需要把链上的所有控件的垂直方向上的约束添加上即可

        // 先把最高的那一个tab 固定好


        view = mAdapter.getHolderByPosition(maxTabHeightIndex).mItemView;

        constraintSet.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        constraintSet.connect(view.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        ids[maxTabHeightIndex] = view.getId();
        previousId = view.getId();


        if (maxTabHeightIndex > 0) {
            for (int i = maxTabHeightIndex - 1; i >= 0; i--) {
                view = mAdapter.getHolderByPosition(i).mItemView;
                constraintSet.connect(view.getId(), ConstraintSet.BOTTOM, previousId, ConstraintSet.BOTTOM);
                previousId = view.getId();
                ids[i] = view.getId();
            }

        }


        if (maxTabHeightIndex < mAdapter.getCount() - 1) {
            for (int i = maxTabHeightIndex + 1; i < mAdapter.getCount(); i++) {
                view = mAdapter.getHolderByPosition(i).mItemView;
                constraintSet.connect(view.getId(), ConstraintSet.BOTTOM, previousId, ConstraintSet.BOTTOM);
                previousId = view.getId();
                ids[i] = view.getId();
            }
        }


        // 第一个参数: 你这个链的左端需要连接到的控件的Id
        // 第二个参数: 你这个链的左端需要连接到第一个参数指定的控件的那一边
        // 第三个参数: 你这个链的右端需要连接大的控件的Id
        // 第四个参数: 你这个链的右端需要连接到第三个参数指定的控件的那一边
        // 第五个参数: 你这个链上多有控件的Id 的一个数组
        // 第六个参数: 权重
        // 第七个参数: 链的模式
        constraintSet.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, ids, null, ConstraintSet.CHAIN_SPREAD_INSIDE);

        constraintSet.applyTo(this);


        // 通过设置 tab 的 padding  来扩大点击事件区域

        int padding = Math.max(mMarginBottom, mMarginTop);
        int paddingOffset = Math.min(minTabWidth / 2, Math.min(mMarginLeft, mMarginRight));
        int paddingTop = mHasDeliverLine ? padding + mDeliverLineWidth : padding;
        for (int i = 0; i < mAdapter.getCount(); i++) {
            mAdapter.getHolderByPosition(i).mItemView.setPadding(paddingOffset, paddingTop, paddingOffset, padding);
        }

        //
        setPadding(mMarginLeft - paddingOffset, 0, mMarginRight - paddingOffset, 0);


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mHasDeliverLine) {
            canvas.drawLine(0, 0, getWidth(), 0, mLinePaint);
        }

    }

    public static class SimpleNavigationAdapter implements NavigationAdapter<SimpleNavigationAdapter.SimpleTabHolder> {

        private int mId = 1000;

        private ArrayList<TabData> mTabData;
        private ArrayList<SimpleTabHolder> mHolders = new ArrayList<>();
        private boolean isFirst = true;

        private CheckBox mPreCheckedTab;

        SimpleNavigationAdapter(ArrayList<TabData> tabData) {
            this.mTabData = tabData;
        }

        @Override
        public SimpleTabHolder createHolder(ViewGroup parent, int position) {

            final BottomNavigation navigation = ((BottomNavigation) parent);
            CheckBox tabView = new CheckBox(parent.getContext());
            tabView.setTag(position);
            tabView.setId(mId + position);
            tabView.setButtonDrawable(null);
            tabView.setGravity(Gravity.CENTER);
            tabView.setTextColor(navigation.mTextColorStateList);
            int textSize = navigation.mTextSize;
            if (textSize > 0) {
                tabView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            }


            tabView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (navigation.mTabSelectedListener != null) {

                        if (isChecked) {

                            if (mPreCheckedTab != buttonView) {
                                navigation.mTabSelectedListener.onTabSelect(buttonView, (Integer) buttonView.getTag());

                                if (mPreCheckedTab != null) {
                                    navigation.mTabSelectedListener.onTabUnSelect(mPreCheckedTab, (Integer) mPreCheckedTab.getTag());

                                    CheckBox temp = mPreCheckedTab;
                                    mPreCheckedTab = (CheckBox) buttonView;
                                    temp.setChecked(false);
                                    return;

                                }

                                mPreCheckedTab = (CheckBox) buttonView;
                            }

                        } else {

                            if (mPreCheckedTab == buttonView) {
                                navigation.mTabSelectedListener.onTabReSelected(buttonView, (Integer) buttonView.getTag());
                                mPreCheckedTab.setChecked(true);
                            }

                        }

                    }
                }
            });


            SimpleTabHolder holder = new SimpleTabHolder(tabView);

            mHolders.add(holder);
            return holder;
        }

        private void select(int position) {
            getHolderByPosition(position).mItemView.setChecked(true);
        }

        @Override
        public void bindData(SimpleTabHolder holder, int position) {

            Drawable topDrawable = holder.mItemView.getContext().getResources().getDrawable(mTabData.get(position).getDrawableId());

            BottomNavigation navigation = (BottomNavigation) holder.mItemView.getParent();

            if (navigation.mDrawableIconWidth > 0 && navigation.mDrawableIconHeight > 0) {
                topDrawable.setBounds(0, 0, navigation.mDrawableIconWidth, navigation.mDrawableIconHeight);
                holder.mItemView.setCompoundDrawables(null, topDrawable, null, null);
            } else {
                holder.mItemView.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
            }


            holder.mItemView.setText(mTabData.get(position).getTitle());


            if (navigation.mCurrentPosition == position) {
                holder.mItemView.setChecked(true);
                if(navigation.mTabSelectedListener == null){
                    mPreCheckedTab = holder.mItemView;
                }

            }


        }

        @Override
        public int getCount() {
            return mTabData == null ? 0 : mTabData.size();
        }


        @Override
        public SimpleTabHolder getHolderByPosition(int position) {
            return mHolders.size() == 0 ? null : mHolders.get(position);
        }

        static class SimpleTabHolder extends TabHolder<CheckBox> {

            SimpleTabHolder(CheckBox itemView) {
                super(itemView);
            }

        }

    }


    public static abstract class TabHolder<T extends View> {
       protected T mItemView;

       public TabHolder(T itemView) {
            this.mItemView = itemView;
        }

    }

    public interface NavigationAdapter<TH extends TabHolder> {

        TH createHolder(ViewGroup parent, int position);

        void bindData(TH holder, int position);

        int getCount();

        TH getHolderByPosition(int position);

    }

    private static class TabData {
        private int drawableId;
        private String title;

        TabData(int drawableId, String title) {
            this.drawableId = drawableId;
            this.title = title;
        }

        int getDrawableId() {
            return drawableId;
        }

        String getTitle() {
            return title;
        }
    }

    public interface OnTabSelectedListener {

        void onTabSelect(View tab, int position);

        void onTabUnSelect(View tab, int position);

        void onTabReSelected(View tab, int position);
    }

    public static  int dip2px(Context context,float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

自定义属性

在res文件下values中添加attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BottomNavigation">
        <attr name="bottomNavigationLeftMargin" format="dimension|reference"/>
        <attr name="bottomNavigationRightMargin" format="dimension|reference"/>
        <attr name="bottomNavigationTopMargin" format="dimension|reference"/>
        <attr name="bottomNavigationBottomMargin" format="dimension|reference"/>
        <attr name="bottomNavigationDrawableMargin" format="dimension|reference"/>
        <attr name="bottomNavigationDrawableWidth" format="dimension|reference"/>
        <attr name="bottomNavigationDrawableHeight" format="dimension|reference"/>
        <attr name="bottomNavigationDeliverLine" format="boolean"/>
        <attr name="bottomNavigationDeliverLineWidth" format="dimension|reference"/>
        <attr name="bottomNavigationDeliverLineColor" format="color|reference"/>


        <attr name="bottomNavigationTextColor" format="color|reference"/>
        <attr name="bottomNavigationTextSize" format="dimension|reference"/>
    </declare-styleable>


    <declare-styleable name="MarqueeView">
        <attr name="textSize" format="dimension" />
        <attr name="textColor" format="color" />
        <attr name="speed" format="integer" />
        <attr name="spacing" format="dimension" />
    </declare-styleable>
</resources>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355