<android> 底部tab按钮 工具类 BootomTabUtils

底部tab 工具类  BootomTabUtils

 

效果:

 

 

使用:

//创建底部tab
String[] text_bottom = {"主页", "View", "进阶", "我的直学"};
int[] seled = {R.mipmap.main_nav_icon1_select, R.mipmap.main_nav_icon2_select_temp, R.mipmap.main_nav_icon3_select_temp, R.mipmap.main_nav_icon4_select};

int[] disseled = {R.mipmap.main_nav_icon1_unselect, R.mipmap.main_nav_icon2_unselect_temp, R.mipmap.main_nav_icon3_unselect_temp, R.mipmap.main_nav_icon4_unselect};

BootomTabUtils bootomTabUtils = new BootomTabUtils();
bootomTabUtils.editBootomView(tilebar_view, this, seled, disseled, text_bottom, 0);
bootomTabUtils.listenerClick(new BootomTabUtils.Listener() {
    @Override
    public void clickTab1(View view, int tabIndex, String tabTest) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame, home_fragment).commit();
    }

    @Override
    public void clickTab2(View view, int tabIndex, String tabTest) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        catory_fragment = new CatoryFragment();
        fragmentTransaction.replace(R.id.frame, catory_fragment).commit();
        hideTitleBar();
    }

    @Override
    public void clickTab3(View view, int tabIndex, String tabTest) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        liveFragment = new LiveFragment();
        fragmentTransaction.replace(R.id.frame, liveFragment).commit();
    }

    @Override
    public void clickTab4(View view, int tabIndex, String tabTest) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        userFragment = new UserFragment();
        fragmentTransaction.replace(R.id.frame, userFragment).commit();
    }
});

 

 

实现:

/*
 * Created by 刘国 on 2018/7/30.
 * 底部tab封装类
 */

public class BootomTabUtils {

    private String LOG = "BootomTabUtils_Log";
    private LinearLayout bottom_tab;

    public BootomTabUtils editBootomView(LinearLayout view, Activity context, int[] selectedIcon, int[] disSelectedIcon, String[] text_bottom, int defaultIndex) {

        if ((selectedIcon.length == 4) && (disSelectedIcon.length == 4) && (text_bottom.length == 4)) {

            bottom_tab = new LinearLayout(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            bottom_tab.setOrientation(LinearLayout.HORIZONTAL);
            bottom_tab.setLayoutParams(layoutParams);
//            bottom_tab.setBackgroundColor(Color.parseColor("#EAEBEC"));
            bottom_tab.setBackgroundColor(Color.parseColor("#ffffff"));

            for (int i = 0; i < selectedIcon.length; i++) {
                LinearLayout ll = new LinearLayout(context);
                LinearLayout.LayoutParams ll_layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
                ll_layoutParams.weight = 1f;
                ll_layoutParams.setMargins(2, 12, 2, 2);
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setLayoutParams(ll_layoutParams);
                ImageView imageView = new ImageView(context);
                TextView textView = new TextView(context);
                textView.setText(text_bottom[i]);
                ll.addView(imageView);
                ll.addView(textView, ll_layoutParams);
                textView.setGravity(Gravity.CENTER);

                if (i == defaultIndex) {
                    imageView.setImageResource(selectedIcon[i]);
                    textView.setTextColor(Color.parseColor("#FE671E"));
                } else {
                    imageView.setImageResource(disSelectedIcon[i]);
                }
                bottom_tab.addView(ll, i);
            }

            addBootomView(view, context);

            addClickListener(bottom_tab, text_bottom, selectedIcon, disSelectedIcon, context);

        } else {

            Log.e(LOG, "ERROR:您editBootomView方法入参的数组长度不相等");
            return this;
        }

        return this;
    }

    /**
     * 做点击ui改变
     * @param index
     * @param selectedIcon
     * @param disSelectedIcon
     */
    private void doforView(int index, int[] selectedIcon, int[] disSelectedIcon) {
        int childCount = bottom_tab.getChildCount();

        for (int i = 0; i < childCount; i++) {
            LinearLayout ll = (LinearLayout) bottom_tab.getChildAt(i);
            if (index == i) {
                ImageView imageView = (ImageView) ll.getChildAt(0);
                TextView textView = (TextView) ll.getChildAt(1);
                imageView.setImageResource(selectedIcon[index]);
                textView.setTextColor(Color.parseColor("#FE671E"));
            } else {
                ImageView imageView = (ImageView) ll.getChildAt(0);
                TextView textView = (TextView) ll.getChildAt(1);
                imageView.setImageResource(disSelectedIcon[i]);
                textView.setTextColor(Color.parseColor("#8A000000"));

            }
        }

    }


    /**
     * 添加监听
     *
     * @param bottom_tab
     * @param text_bottom
     * @param selectedIcon
     * @param disSelectedIcon
     * @param context
     */
    private void addClickListener(LinearLayout bottom_tab, final String[] text_bottom, final int[] selectedIcon, final int[] disSelectedIcon, final Activity context) {

        int childCount = bottom_tab.getChildCount();
        for (int i = 0; i < childCount; i++) {

            View childAt = bottom_tab.getChildAt(i);
            final int finalI = i;
            childAt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    switch (finalI) {
                        case 0:
                            doforView(0, selectedIcon, disSelectedIcon);
                            listener.clickTab1(view, finalI, text_bottom[finalI]);
                            break;
                        case 1:
                            doforView(1, selectedIcon, disSelectedIcon);
                            listener.clickTab2(view, finalI, text_bottom[finalI]);
                            break;
                        case 2:
                            doforView(2, selectedIcon, disSelectedIcon);
                            listener.clickTab3(view, finalI, text_bottom[finalI]);
                            break;
                        case 3:
                            doforView(3, selectedIcon, disSelectedIcon);
                            listener.clickTab4(view, finalI, text_bottom[finalI]);
                            break;
                    }
                }
            });
        }
    }


    /**
     * 添加底部tab
     */
    public BootomTabUtils addBootomView(LinearLayout view, Activity context) {
        if (bottom_tab != null) {
            RelativeLayout linearLayout = new RelativeLayout(context);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);//两个参数分别是layout_width,layout_height
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            linearLayout.setLayoutParams(layoutParams);
            linearLayout.addView(view);
            linearLayout.addView(bottom_tab, layoutParams);
            context.setContentView(linearLayout);
        } else {

            Log.e(LOG, "ERROR:editBootomView方法要在addBootomView方法前调用");
        }
        return this;
    }


    public Listener listener;

    public interface Listener {
        public abstract void clickTab1(View view, int tabIndex, String tabTest);

        public abstract void clickTab2(View view, int tabIndex, String tabTest);

        public abstract void clickTab3(View view, int tabIndex, String tabTest);

        public abstract void clickTab4(View view, int tabIndex, String tabTest);
    }

    public void listenerClick(Listener listener) {
        this.listener = listener;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值