• 通用型的底部导航栏示例


    废话不多说,上图:

    几乎所有的应用app都有这么一块导航栏。现在把最省心的一种写法放到这里,自己的轮子,用着放心。

    关键代码如下:

    BottomAdapter.java

      1 package com.example.gc_hank.evolutionpro.ui.bottom;
      2 
      3 import android.app.Activity;
      4 import android.content.Context;
      5 import android.graphics.drawable.Drawable;
      6 import android.support.annotation.NonNull;
      7 import android.support.v7.widget.RecyclerView;
      8 import android.util.DisplayMetrics;
      9 import android.view.LayoutInflater;
     10 import android.view.View;
     11 import android.view.ViewGroup;
     12 import android.view.WindowManager;
     13 import android.widget.ImageView;
     14 import android.widget.LinearLayout;
     15 import android.widget.TextView;
     16 
     17 import com.example.gc_hank.evolutionpro.R;
     18 
     19 import java.util.List;
     20 
     21 import butterknife.BindView;
     22 import butterknife.ButterKnife;
     23 
     24 /**
     25  * 底部导航栏专用RecyclerView.Adapter示例
     26  */
     27 public class BottomAdapter extends RecyclerView.Adapter {
     28 
     29     private List<TabTitle> mTitleObjectList;//
     30     private int mSelectedPosition = 0;
     31     private OnItemClickListener onItemClickListener;
     32 
     33     /**
     34      * 对外接口
     35      */
     36     public interface OnItemClickListener {
     37         void onItemClick(int position);
     38     }
     39 
     40     private Activity mActivity;
     41     private int itemWidth;
     42 
     43     public BottomAdapter(Activity activity, List<TabTitle> titleObjectList) {
     44         mActivity = activity;
     45         mTitleObjectList = titleObjectList;
     46     }
     47 
     48     @NonNull
     49     @Override
     50     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
     51         View view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_list_item_bottom_title, null);
     52         initItemWidth();
     53         return new BottomHolder(view);
     54     }
     55 
     56     @Override
     57     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
     58         BottomHolder bHolder = (BottomHolder) viewHolder;
     59 
     60         int titleNameId = mTitleObjectList.get(position).getTitleName();
     61         Drawable titleIconDrawable = mTitleObjectList.get(position).getDrawable();
     62 
     63         bHolder.tvTitle.setTextColor(mActivity.getResources().getColorStateList(mTitleObjectList.get(position).getTextColorStateList()));
     64         bHolder.tvTitle.setText(mActivity.getResources().getString(titleNameId));
     65 
     66         bHolder.ivIcon.setImageDrawable(titleIconDrawable);
     67 
     68         bHolder.llMain.getLayoutParams().width = itemWidth;
     69         bHolder.llMain.getLayoutParams().height = (int) mActivity.getResources().getDimension(R.dimen.height_b);
     70 
     71         bHolder.llMain.requestLayout();//由于要强制等分,所以,要重新布局一下,调整宽高
     72 
     73         if (mSelectedPosition == position) {// 如果当前tab被选中了
     74             bHolder.ivIcon.setSelected(true);
     75             bHolder.tvTitle.setSelected(true);
     76         } else {
     77             bHolder.ivIcon.setSelected(false);
     78             bHolder.tvTitle.setSelected(false);
     79         }
     80 
     81         bHolder.llMain.setOnClickListener(new View.OnClickListener() {
     82             @Override
     83             public void onClick(View v) {
     84                 setSelection(position);//先改变当前的item的选中情况
     85                 if (null != onItemClickListener)
     86                     onItemClickListener.onItemClick(position);//再执行外界传入的点击事件
     87             }
     88         });
     89     }
     90 
     91     /**
     92      * 让每一个item等分
     93      */
     94     private void initItemWidth() {
     95         WindowManager windowManager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);// 得到当前屏幕的宽度,然后分成getItemCount等分,让每个item平均分配
     96         DisplayMetrics metrics = new DisplayMetrics();
     97         windowManager.getDefaultDisplay().getMetrics(metrics);
     98         int w = metrics.widthPixels;//这就是宽度像素值
     99         itemWidth = w / getItemCount();
    100     }
    101 
    102     @Override
    103     public int getItemCount() {
    104         return mTitleObjectList == null ? 0 : mTitleObjectList.size();
    105     }
    106 
    107     @Override
    108     public int getItemViewType(int position) {
    109         return super.getItemViewType(position);
    110     }
    111 
    112     class BottomHolder extends RecyclerView.ViewHolder {
    113 
    114         @BindView(R.id.tv_title)
    115         TextView tvTitle;
    116         @BindView(R.id.iv_icon)
    117         ImageView ivIcon;
    118         @BindView(R.id.ll_main)
    119         LinearLayout llMain;
    120 
    121         public BottomHolder(@NonNull View itemView) {
    122             super(itemView);
    123             ButterKnife.bind(this, itemView);
    124         }
    125     }
    126 
    127     //```````````以下全都是对外接口····················//
    128     public void setSelection(int position) {
    129         this.mSelectedPosition = position;
    130         notifyDataSetChanged();
    131     }
    132 
    133     public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    134         this.onItemClickListener = onItemClickListener;
    135     }
    136 
    137 
    138 }
    View Code

    TabTitle.java

     1 package com.example.gc_hank.evolutionpro.ui.bottom;
     2 
     3 import android.graphics.drawable.StateListDrawable;
     4 
     5 public class TabTitle {
     6     private int titleName;
     7     private int textColorStateList;
     8     private StateListDrawable drawable;
     9 
    10     /**
    11      * 底部导航栏配置类
    12      *
    13      * @param titleName          标题text
    14      * @param textColorStateList 标题颜色selector
    15      * @param drawable           标题icon 的selector
    16      */
    17     public TabTitle(int titleName, int textColorStateList, StateListDrawable drawable) {
    18         this.titleName = titleName;
    19         this.drawable = drawable;
    20         this.textColorStateList = textColorStateList;
    21     }
    22 
    23     public int getTitleName() {
    24         return titleName;
    25     }
    26 
    27     public StateListDrawable getDrawable() {
    28         return drawable;
    29     }
    30 
    31     public int getTextColorStateList() {
    32         return textColorStateList;
    33     }
    34 
    35 
    36 }
    View Code

    MainActivity.java

    package com.example.gc_hank.evolutionpro.ui;
    
    import android.graphics.drawable.StateListDrawable;
    import android.os.Bundle;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    
    import com.example.gc_hank.evolutionpro.R;
    import com.example.gc_hank.evolutionpro.ui.bottom.BottomAdapter;
    import com.example.gc_hank.evolutionpro.ui.bottom.TabTitle;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    
    public class MainActivity extends AppCompatActivity {
    
        @BindView(R.id.rv_bottom)
        RecyclerView mRvBottom;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            initBottom();
        }
    
        private void initBottom() {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRvBottom.setLayoutManager(linearLayoutManager);
    
            final BottomAdapter adapter = new BottomAdapter(this, getSetting());
            adapter.setOnItemClickListener(new BottomAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    adapter.setSelection(position);
                    //TODO 并且这里要切换Fragment
                    Log.d("onItemClickTag", "" + position);
                }
            });
            mRvBottom.setAdapter(adapter);
        }
    
        //
        private List<TabTitle> getSetting() {
            List<TabTitle> titleObjectList = new ArrayList<>();
            titleObjectList.add(new TabTitle(R.string.tag_name_home, R.color.home_tab_text_selector, getStateListDrawable(R.mipmap.a_tabbar_home, R.mipmap.a_tabbar_home_p)));
            titleObjectList.add(new TabTitle(R.string.tag_name_quote, R.color.home_tab_text_selector, getStateListDrawable(R.mipmap.a_tabbar_market, R.mipmap.a_tabbar_market_p)));
            titleObjectList.add(new TabTitle(R.string.tag_name_deal, R.color.home_tab_text_selector, getStateListDrawable(R.mipmap.a_tabbar_trade, R.mipmap.a_tabbar_trade_p)));
            titleObjectList.add(new TabTitle(R.string.tag_name_mine, R.color.home_tab_text_selector, getStateListDrawable(R.mipmap.a_tabbar_me, R.mipmap.a_tabbar_me_p)));
            return titleObjectList;
        }
    
        /**
         * 返回一个Drawable对象,可以根据selected状态改变 图标
         *
         * @param normalIcon
         * @param selectedIcon
         * @return
         */
        private StateListDrawable getStateListDrawable(int normalIcon, int selectedIcon) {
            StateListDrawable drawable = new StateListDrawable();
            drawable.addState(new int[]{android.R.attr.state_selected}, ContextCompat.getDrawable(this, selectedIcon));//选中之后的drawable
            drawable.addState(new int[]{}, ContextCompat.getDrawable(this, normalIcon));//正常情况下的drawable
            return drawable;
        }
    }
    View Code

    然后是资源文件:

    home_tab_text_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#2A60F5" android:state_checked="true" />
        <item android:color="#2A60F5" android:state_selected="true" />
        <item android:color="#8D939B" />
    </selector>
    View Code

    最后给出github的地址:

    https://github.com/18598925736/CommonBottomBar

    下载可直接运行

  • 相关阅读:
    BZOJ4004: [JLOI2015]装备购买
    POJ3696:The Luckiest number
    BZOJ1053: [HAOI2007]反素数ant
    BZOJ1029: [JSOI2007]建筑抢修
    牛站(贪心+暴力做法)
    浅谈SPFA(没有特别的探讨,只是对某天晚上的思考做个记录)
    火车进栈问题(如何快速计算单个组合数)
    雷达设备
    畜栏预定
    防晒
  • 原文地址:https://www.cnblogs.com/hankzhouAndroid/p/10110125.html
Copyright © 2020-2023  润新知