美文网首页
自定义TabMenu方便使用

自定义TabMenu方便使用

作者: Json_杰 | 来源:发表于2017-11-17 14:34 被阅读0次

    开发过程中我们经常会这样的需求,搭建项目框架的时候需要创建低下四个Tab,刚好项目空闲时间,再次为大家抽离了自己项目中封装的Tab 方便大家使用 调用,有很好的定制型,可以根据项目的需求自己实现自己想要的效果。

    packagecom.fanwe.library.view;

    importandroid.content.Context;

    importandroid.graphics.Color;

    importandroid.util.AttributeSet;

    importandroid.widget.ImageView;

    importandroid.widget.TextView;

    importcom.fanwe.library.R;

    importcom.fanwe.library.utils.SDViewBinder;

    importcom.fanwe.library.view.select.SDSelectViewAuto;

    public classSDTabMenuextendsSDSelectViewAuto

    {

    publicImageViewmIvTitle;

    publicTextViewmTvTitle;

    publicTextViewmTvNumbr;

    publicSDTabMenu(Context context,AttributeSet attrs)

    {

    super(context,attrs);

    init();

    }

    publicSDTabMenu(Context context)

    {

    super(context);

    init();

    }

    @Override

    protected voidinit()

    {

    setContentView(R.layout.view_tab_menu);

    mIvTitle= (ImageView) findViewById(R.id.iv_title);

    mTvTitle= (TextView) findViewById(R.id.tv_title);

    mTvNumbr= (TextView) findViewById(R.id.tv_number);

    addAutoView(mIvTitle,mTvNumbr,mTvTitle);

    setDefaultConfig();

    onNormal();

    setTextTitleNumber(null);

    super.init();

    }

    @Override

    public voidsetDefaultConfig()

    {

    getViewConfig(mTvTitle).setTextColorNormal(Color.GRAY);

    getViewConfig(mTvTitle).setTextColorSelected(mLibraryConfig.getmMainColor());

    super.setDefaultConfig();

    }

    public voidsetTextTitleNumber(String content)

    {

    SDViewBinder.setTextViewsVisibility(mTvNumbr,content);

    }

    public voidsetTextTitle(String content)

    {

    SDViewBinder.setTextViewsVisibility(mTvTitle,content);

    }

    public voidsetBackgroundTextTitleNumber(intresId)

    {

    mTvNumbr.setBackgroundResource(resId);

    }

    public  voidsetVisibilityTextTitleNumber(intvisibility){

    mTvNumbr.setVisibility(visibility);

    }

    }

    //布局如下

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/white"

    android:orientation="vertical">

    android:id="@+id/act_main_fl_content"

    android:layout_width="match_parent"

    android:layout_height="0dp"

    android:layout_weight="1">

    android:id="@+id/act_main_ll_bottom_menu"

    android:layout_width="match_parent"

    android:layout_height="50dp"

    android:orientation="horizontal">

    android:id="@+id/tab0"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:background="@drawable/selector_white_gray_stroke_top"/>

    android:id="@+id/tab1"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:background="@drawable/selector_white_gray_stroke_top"/>

    android:id="@+id/tab2"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:background="@drawable/selector_white_gray_stroke_top"/>

    android:id="@+id/tab3"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:background="@drawable/selector_white_gray_stroke_top"/>

    android:id="@+id/tab4"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:visibility="gone"

    android:background="@drawable/selector_white_gray_stroke_top"/>

    //布局就是简单的一个线性布局包含了 一个帧布局和一个线性布局里面包含我们自定义的Tab,这种也比较方便,这样只是减少繁琐的添加取消有一定的灵活性

    //接下来就是我们最主要的如何使用了

    最基础的地方就省略

    private voidinitBottom()

    {

    mTab0.setBackgroundTextTitleNumber(R.drawable.bg_number);

    mTab1.setBackgroundTextTitleNumber(R.drawable.bg_number);

    mTab2.setBackgroundTextTitleNumber(R.drawable.bg_number);

    mTab3.setBackgroundTextTitleNumber(R.drawable.bg_number);

    mTab4.setBackgroundTextTitleNumber(R.drawable.bg_number);

    mTab0.setTextTitle(SDResourcesUtil.getString(R.string.home));

    mTab1.setTextTitle(SDResourcesUtil.getString(R.string.supplier));

    mTab2.setTextTitle(SDResourcesUtil.getString(R.string.shopcart));

    mTab3.setTextTitle(SDResourcesUtil.getString(R.string.mine));

    mTab4.setTextTitle(SDResourcesUtil.getString(R.string.more));

    mTab0.getViewConfig(mTab0.mIvTitle).setImageNormalResId(R.drawable.button_home_close).setImageSelectedResId(R.drawable.butt_home_open);

    mTab1.getViewConfig(mTab1.mIvTitle).setImageNormalResId(R.drawable.button_market_close).setImageSelectedResId(R.drawable.button_market_open);

    mTab2.getViewConfig(mTab2.mIvTitle).setImageNormalResId(R.drawable.button_shopping_close).setImageSelectedResId(R.drawable.button_shopping_open);

    mTab3.getViewConfig(mTab3.mIvTitle).setImageNormalResId(R.drawable.button_my_close).setImageSelectedResId(R.drawable.button_my_open);

    mTab4.getViewConfig(mTab4.mIvTitle).setImageNormalResId(R.drawable.tab_4_normal).setImageSelectedResId(R.drawable.tab_4_press);

    mTab0.getViewConfig(mTab0.mTvTitle).setTextColorNormalResId(R.color.text_home_menu_normal)

    .setTextColorSelectedResId(R.color.text_home_menu_selected);

    mTab1.getViewConfig(mTab1.mTvTitle).setTextColorNormalResId(R.color.text_home_menu_normal)

    .setTextColorSelectedResId(R.color.text_home_menu_selected);

    mTab2.getViewConfig(mTab2.mTvTitle).setTextColorNormalResId(R.color.text_home_menu_normal)

    .setTextColorSelectedResId(R.color.text_home_menu_selected);

    mTab3.getViewConfig(mTab3.mTvTitle).setTextColorNormalResId(R.color.text_home_menu_normal)

    .setTextColorSelectedResId(R.color.text_home_menu_selected);

    mTab4.getViewConfig(mTab4.mTvTitle).setTextColorNormalResId(R.color.text_home_menu_normal)

    .setTextColorSelectedResId(R.color.text_home_menu_selected);

    mViewManager.setListener(newSDSelectManagerListener()

    {

    @Override

    public voidonNormal(intindex,SDTabMenu item)

    {

    }

    @Override

    public voidonSelected(intindex,SDTabMenu item)

    {

    mSelectedIndex= index;

    switch(index)

    {

    case0:

    click0();

    mTab2.setVisibilityTextTitleNumber(View.GONE);

    break;

    case1:

    click1();

    mTab2.setVisibilityTextTitleNumber(View.GONE);

    break;

    case2:

    click2();

    mTab2.setVisibilityTextTitleNumber(View.VISIBLE);

    break;

    case3:

    click3();

    mTab2.setVisibilityTextTitleNumber(View.GONE);

    break;

    case4:

    click4();

    break;

    default:

    break;

    }

    }

    });

    SDTabMenu[] items =newSDTabMenu[] {mTab0,mTab1,mTab2,mTab3,mTab4};

    mViewManager.setItems(items);

    mViewManager.performClick(mSelectedIndex);

    }

    //对应你需要的Tab做相应的处理  显示隐藏  或者你可以在基类添加一些你需要的东西扩展。

    相关文章

      网友评论

          本文标题:自定义TabMenu方便使用

          本文链接:https://www.haomeiwen.com/subject/xuylmxtx.html