安卓底部导航栏点击变色切换不同Fragment

导航栏效果“`



public class BottomNavigationBar extends LinearLayout {
    private Context context;

    public BottomNavigationBar(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * 底部的显示模式
     */
    public enum NavShowType {
        NORMAL, //普通模式,包含文字和图片
        NO_TEXT, //无文字模式
        NO_IMAGE, //无图模式
        CHECKED_SHOW_TEXT //普通无文字 选中出现文字
    }

    public static final int DEFAULT_UNCHECKED_IMAGE = R.drawable.ic_android_black_24dp;
    public static final int DEFAULT_CHECKED_IMAGE = R.drawable.ic_android_green_24dp;

    public static final int DEFAULT_CHECKED_TEXT = 0xff138768;
    public static final int DEFAULT_UNCHECKED_TEXT = 0xff747575;

    NavShowType type = NavShowType.NORMAL;


    public void setType(NavShowType type) {
        this.type = type;
        boolean tv = true, iv = true;
        switch (type) {
            case NORMAL:
                tv = true;
                iv = true;
                break;
            case NO_IMAGE:
                tv = true;
                iv = false;
                break;
            case NO_TEXT:
                tv = false;
                iv = true;
                break;
            case CHECKED_SHOW_TEXT:
                tv = false;
                iv = true;
        }
        for (NavigationItem item : items) {
            item.setTextVisible(tv);
            item.setImageVisible(iv);
            item.showView();
        }
        if (type == NavShowType.CHECKED_SHOW_TEXT) {
            items.get(currentChecked).setTextVisible(true);
        }
    }

    //当前被选中的元素
    int currentChecked = 0;

    public BottomNavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationBar);
        String t = a.getString(R.styleable.BottomNavigationBar_show_type);
        if (t == null) {
            setType(NavShowType.NORMAL);
        } else {
            if (t.equals("1")) {
                setType(NavShowType.NO_TEXT);

            } else if (t.equals("2")) {
                setType(NavShowType.NO_IMAGE);

            } else if (t.equals("3")) {
                setType(NavShowType.CHECKED_SHOW_TEXT);

            } else if (t.equals("0")) {
                setType(NavShowType.NORMAL);

            }
        }

        textCheckdColor = a.getColor(R.styleable.BottomNavigationBar_textCheckedColor, textCheckdColor);
        textUnCheckColor = a.getColor(R.styleable.BottomNavigationBar_textUnCheckedColor, textUnCheckColor);
        a.recycle();
    }

    public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

    }


    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
    }

    public void addItemView(final NavigationItem item) {
        boolean tv = true, iv = true;
        switch (type) {
            case NORMAL:
                tv = true;
                iv = true;
                break;
            case NO_IMAGE:
                tv = true;
                iv = false;
                break;
            case NO_TEXT:
                tv = false;
                iv = true;
                break;
            case CHECKED_SHOW_TEXT:
                tv = false;
                iv = true;
        }
        item.setTextVisible(tv);
        item.setImageVisible(iv);
        item.setTextCheckedColor(textCheckdColor);
        item.setTextUnCheckedColor(textUnCheckColor);
        items.add(item);
        final int i = items.size() - 1;
        item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //这个item在集合中的第几个
                int index = i;
                //如果不是当前的view
                boolean noChecked = index != currentChecked;
                if (noChecked) {
                    //改变上一个view
                    items.get(currentChecked).changeView();
                    if (type == NavShowType.CHECKED_SHOW_TEXT) {
                        items.get(currentChecked).setTextVisible(false);
                    }
                    currentChecked = index;
                    if (onItemViewSelectedListener != null) {
                        onItemViewSelectedListener.onItemClcik(v, index);
                    }
                    //改变当前点击的view
                    item.changeView();
                    if (type == NavShowType.CHECKED_SHOW_TEXT) {
                        item.setTextVisible(true);
                    }

                }

            }
        });
        //Item 之间的分割线
//        TextView tv_line = new TextView(context);
//        LayoutParams lineParams = new LayoutParams(2, ViewGroup.LayoutParams.MATCH_PARENT);
//        lineParams.setMargins(0, 15, 0, 15);
//        tv_line.setLayoutParams(lineParams);
//        tv_line.setBackgroundColor(getResources().getColor(R.color.color_dcdcdc));
//

        addView(item);
//        addView(tv_line);
        if (items.size() == 1) {
            item.changeView();
        }
        item.showView();
    }

    int textCheckdColor = DEFAULT_CHECKED_TEXT;
    int textUnCheckColor = DEFAULT_UNCHECKED_TEXT;


    /**
     * 增加
     *
     * @param text
     */
    public void addItemView(String text, int textCheckdColor, int textUnCheckColor, int imageCheckedResource, int imageUnCheckedResource) {
        NavigationItem item = new NavigationItem(getContext(), textCheckdColor, textUnCheckColor, imageCheckedResource, imageUnCheckedResource);
        item.getmTextView().setText(text);
        addItemView(item);
    }

    public void setTextColor(int checkedColor, int unCheckedColor) {
        this.textCheckdColor = checkedColor;
        this.textUnCheckColor = unCheckedColor;
        for (NavigationItem item : items) {
            item.setTextCheckedColor(checkedColor);
            item.setTextUnCheckedColor(unCheckedColor);
            item.showView();
        }
    }

    public void setTextColor(int checkedColor) {
        this.textCheckdColor = checkedColor;
        for (NavigationItem item : items) {
            item.setTextCheckedColor(checkedColor);
            item.setTextUnCheckedColor(textUnCheckColor);
            item.showView();
        }
    }



    public void setTextColorRes(int checkedColorId, int unCheckedColorId) {
        setTextColor(getResources().getColor(checkedColorId), getResources().getColor(unCheckedColorId));
    }

    public void setTextColorRes(int checkedColorId) {
        setTextColor(getResources().getColor(checkedColorId));
    }

//    public void addItemView(String text) {
//        NavigationItem item = new NavigationItem(getContext(), textCheckdColor, textUnCheckColor, DEFAULT_CHECKED_IMAGE, DEFAULT_UNCHECKED_IMAGE);
//        item.getmTextView().setText(text);
//        addItemView(item);
//    }

    public void addItemView(String text, int imageCheckedResource, int imageUnCheckedResource) {
        addItemView(text, textCheckdColor, textUnCheckColor, imageCheckedResource, imageUnCheckedResource);
    }


    OnItemViewSelectedListener onItemViewSelectedListener;

    public void setOnItemViewSelectedListener(OnItemViewSelectedListener onItemViewSelectedListener) {
        this.onItemViewSelectedListener = onItemViewSelectedListener;
    }

    //item列表
    ArrayList<NavigationItem> items = new ArrayList<>();

    public interface OnItemViewSelectedListener {
        void onItemClcik(View v, int index);
    }





        Activity中的代码,其他省略
    /**
     * 底部导航栏添加图标
     */
    private FragmentManager fm;
      private static Fragment homePagerFragment;//本页Fragment
      private static Fragment myOrderFragment;//第二个Fragment
    private void initView() {
        navBar.addItemView(getResources().getString(R.string.底部字), R.mipmap.图1, R.mipmap.图1);
      //有几个添加几个navBar.addItem();
        fm = getSupportFragmentManager();
        initListener();
    }

    /**
     * 初始化第一个fragment
     */
    private void initFragment() {
        loadFragment(0);
    }

    /**
     * 点击
     */
    private void initListener() {
        navBar.setOnItemViewSelectedListener(new BottomNavigationBar.OnItemViewSelectedListener() {
            @Override
            public void onItemClcik(View v, int index) {
                loadFragment(index);
            }
        });
    }

    /**
     * 底部图标切换页面
     * @param index
     */
    private void loadFragment(int index) {

        ft = fm.beginTransaction();
        if (index == 0) {
            if (homePagerFragment == null) {
                homePagerFragment = 本页Fragment.newInstance(getResources().getString(R.string.底部字));
            }
            ft.replace(R.id.container, homePagerFragment);
        }
        if (index == 1) {
            if (myOrderFragment == null) {
                myOrderFragment = 第二个Fragment.newInstance();
            }
           //有几个页面添加几个
        ft.commit();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值