封装通用的底部导航栏

来源:互联网 发布:sql转换成日期格式函数 编辑:程序博客网 时间:2024/05/22 17:46

陆陆续续的也做了几个项目,发现项目中很多东西都是通用的,懒是程序员的一个优点,为了避免重复造轮子的现象,因此一次性将这些轮子造出来,方便以后拿来就用。

先看效果:


这就是封装的一个效果了。大家看我只用了几行代码就实现了这个效果:

public class MainActivity extends BottomTabBaseActivity{    @Override    protected List<BottomTabView.TabItemView> getTabViews() {        List<BottomTabView.TabItemView> tabItemViews = new ArrayList<>();        tabItemViews.add(new BottomTabView.TabItemView(this, "玩酷", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.ku, R.mipmap.ku_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "魔鬼", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.mo, R.mipmap.mo_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "生气", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.sheng, R.mipmap.sheng_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "天使", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.tian, R.mipmap.tian_1));        return tabItemViews;    }    @Override    protected List<Fragment> getFragments() {        List<Fragment> fragments = new ArrayList<>();        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        return fragments;    }    @Override    protected View getCenterView() {        ImageView centerView = new ImageView(this);        centerView.setImageResource(R.mipmap.cury);        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 150);        centerView.setLayoutParams(layoutParams);        centerView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, "centerView 点击了", Toast.LENGTH_SHORT).show();            }        });        return centerView;    }}

当然,这几行代码的背后肯定是更多的代码支持,然后这却可以随处引用,不用重复造轮子了:

通过自定义一个类,动态的添加我们需要的底部导航按钮,主要的两个方法如下:

    /**     * 设置 Tab Item View     */    public void setTabItemViews(List<TabItemView> tabItemViews) {        setTabItemViews(tabItemViews, null);    }    /**     * 设置 Tab Item View     */    public void setTabItemViews(List<TabItemView> tabItemViews, View centerView) {        if (this.tabItemViews != null) {            throw new RuntimeException("不能重复设置!");        }

提供 setOnTabItemSelectListener 方法,用于设置 TabItem 的选择监听,可以实现 ViewPager 的页面切换或者 Fragment 切换,还提供了用于二次点击的方法:

    public void setOnTabItemSelectListener(OnTabItemSelectListener onTabItemSelectListener) {        this.onTabItemSelectListener = onTabItemSelectListener;    }    public void setOnSecondSelectListener(OnSecondSelectListener onSecondSelectListener) {        this.onSecondSelectListener = onSecondSelectListener;    }

基础内容介绍完了,开撸:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:clipChildren="false"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v4.view.ViewPager        android:id="@+id/viewPager"        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    <View        android:layout_width="match_parent"        android:background="#c7c7c7"        android:layout_height="0.1dp"/>    <com.example.forget.bottomview.BottomTabView        android:id="@+id/bottomTabView"        android:gravity="bottom"        android:layout_width="match_parent"        android:layout_height="50dp"/></LinearLayout>

(布局文件,就是一个viewpager,以及一个自定义的view)

BottomTabBaseActivity 设置成抽象类(用于强制子类实现),并添加三个方法:

    protected abstract List<BottomTabView.TabItemView> getTabViews();    protected abstract List<Fragment> getFragments();    protected View getCenterView(){        return null;    }
这么写的原因如下:

可变的东西(底部菜单的 Item、Fragment)放在子类去实现,父类 BottomTabBaseActivity 实现不变的、重复性的代码
所以声明两个抽象方法 getTabViews、getFragments ,并在子类去实现,至于 getCenterView 方法可以自行定制,如果需要 CenterView 在子类实现方法即可。

MainActivity 继承自 BottomTabBaseActivity,只需实现两个方法,其他的什么也不需要做:
public class MainActivity extends BottomTabBaseActivity{    @Override    protected List<BottomTabView.TabItemView> getTabViews() {        List<BottomTabView.TabItemView> tabItemViews = new ArrayList<>();        tabItemViews.add(new BottomTabView.TabItemView(this, "玩酷", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.ku, R.mipmap.ku_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "魔鬼", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.mo, R.mipmap.mo_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "生气", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.sheng, R.mipmap.sheng_1));        tabItemViews.add(new BottomTabView.TabItemView(this, "天使", R.color.colorPrimary,                R.color.colorAccent, R.mipmap.tian, R.mipmap.tian_1));        return tabItemViews;    }    @Override    protected List<Fragment> getFragments() {        List<Fragment> fragments = new ArrayList<>();        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        fragments.add(new TabFragment1());        return fragments;    }

这样效果就实现了。如果不想让中间的部分暴露出来的话,那么只用修改以下部分即可:

在最外层布局添加属性并设置成     android:clipChildren="true"或者不写这句即可。


原创粉丝点击