<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;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题可能在于您的JavaScript代码中的位置。在您的示例代码中,您正在尝试使用JavaScript删除单元格中的onclick属性,但是该代码位于HTML表格之后。由于JavaScript代码在HTML代码之后加载和执行,所以当您尝试删除onclick属性时,该单元格可能还没有被加载到页面上,因此代码不起作用。 要解决这个问题,您应该将JavaScript代码放在HTML表格之前,或者在页面加载时使用window.onload事件来确保表格和单元格已经加载到页面上。以下是一个示例代码: ``` <script> window.onload = function() { var cell = document.getElementById("cell1"); cell.onclick = null; } </script> <table class="tab_1"> <tr> <td><input type="checkbox" id="select-all"></td> <td>录入时间</td> <td>退货单号</td> <td>录入人</td> <td>备注</td> <td>状态</td> <td>日志</td> </tr> {% for item in data %} <tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');"> <td id="cell1"><input type="checkbox" value="{{item[1]}}" name="id"></td> <td>{{item[0]}}</td> <td><a href="/do_chawuliu2?danhao={{item[1]}}">{{item[1]}}</a></td> <td>{{item[2]}}</td> <td>{{item[3]}}</td> <td>{% if item[4] is none %}{% else %}{{item[4]}}{% endif %}</td> <td>日志</td> </tr> {% endfor %} </table> ``` 在这个例子中,我们将JavaScript代码放在了window.onload事件处理程序中,以确保表格和单元格已经被加载到页面上。现在,当页面加载完成时,JavaScript代码会找到ID为“cell1”的单元格,并将其onclick属性设置为null,从而删除任何之前定义的onclick事件处理程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值