BottomBarLayout.java 6.5 KB
Newer Older
C
chaychan 已提交
1 2 3
package com.chaychan.library;

import android.content.Context;
4
import android.content.res.TypedArray;
C
chaychan 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChayChan
 * @description: 底部页签根节点
 * @date 2017/6/23  11:02
 */
public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageChangeListener {

    private static final String STATE_INSTANCE = "instance_state";
    private static final String STATE_ITEM = "state_item";


    private ViewPager mViewPager;
    private int mChildCount;//子条目个数
    private List<BottomBarItem> mItemViews = new ArrayList<>();
    private int mCurrentItem;//当前条目的索引
    private boolean mSmoothScroll;

    public BottomBarLayout(Context context) {
        this(context, null);
    }

    public BottomBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
42
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarLayout);
C
chaychan 已提交
43
        mSmoothScroll = ta.getBoolean(R.styleable.BottomBarLayout_smoothScroll,false);
44
        ta.recycle();
C
chaychan 已提交
45 46 47 48 49 50 51 52

    }

    @Override
    public void setOrientation(int orientation) {
        super.setOrientation(orientation);
    }

C
chaychan 已提交
53 54
    public void setViewPager(ViewPager viewPager) {
        this.mViewPager = viewPager;
C
chaychan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        init();
    }

    private void init() {
        if (mViewPager == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        mChildCount = getChildCount();
        if (mViewPager.getAdapter().getCount() != mChildCount) {
            throw new IllegalArgumentException("LinearLayout的子View数量必须和ViewPager条目数量一致");
        }
        for (int i = 0; i < mChildCount; i++) {
            if (getChildAt(i) instanceof BottomBarItem) {
                BottomBarItem bottomBarItem = (BottomBarItem) getChildAt(i);
                mItemViews.add(bottomBarItem);
                //设置点击监听
                bottomBarItem.setOnClickListener(new MyOnClickListener(i));
            } else {
74
                throw new IllegalArgumentException("BottomBarLayout的子View必须是BottomBarItem");
C
chaychan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            }
        }

        mItemViews.get(mCurrentItem).setStatus(true);//设置选中项
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        resetState();
        mItemViews.get(position).setStatus(true);
91 92 93 94
        if (onItemSelectedListener != null){
            onItemSelectedListener.onItemSelected(getBottomItem(position),position);
        }
        mCurrentItem = position;//记录当前位置
C
chaychan 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class MyOnClickListener implements OnClickListener {

        private int currentIndex;

        public MyOnClickListener(int i) {
            this.currentIndex = i;
        }

        @Override
        public void onClick(View v) {
            //回调点击的位置
113
            if (onItemSelectedListener != null && currentIndex == mCurrentItem) {
C
chaychan 已提交
114 115 116
                onItemSelectedListener.onItemSelected(getBottomItem(currentIndex),currentIndex);
            }

C
chaychan 已提交
117
            mViewPager.setCurrentItem(currentIndex, mSmoothScroll);
C
chaychan 已提交
118 119 120 121
        }
    }

    /**
穆弋's avatar
穆弋 已提交
122
     * 重置当前按钮的状态
C
chaychan 已提交
123 124
     */
    private void resetState() {
穆弋's avatar
穆弋 已提交
125 126
        if (mCurrentItem < mItemViews.size()){
            mItemViews.get(mCurrentItem).setStatus(false);
C
chaychan 已提交
127 128 129
        }
    }

C
chaychan 已提交
130 131
    public void setCurrentItem(int currentItem) {
        mCurrentItem = currentItem;
C
chaychan 已提交
132 133 134
        mViewPager.setCurrentItem(mCurrentItem,mSmoothScroll);
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    /**
     * 设置未读数
     * @param position 底部标签的下标
     * @param unreadNum 未读数
     */
    public void setUnread(int position,int unreadNum){
        mItemViews.get(position).setUnreadNum(unreadNum);
    }

    /**
     * 设置提示消息
     * @param position 底部标签的下标
     * @param msg 未读数
     */
    public void setMsg(int position,String msg){
        mItemViews.get(position).setMsg(msg);
    }

    /**
     * 隐藏提示消息
     * @param position 底部标签的下标
     */
    public void hideMsg(int position){
        mItemViews.get(position).hideMsg();
    }

    /**
     * 显示提示的小红点
     * @param position 底部标签的下标
     */
    public void showNotify(int position){
        mItemViews.get(position).showNotify();
    }

    /**
     * 隐藏提示的小红点
     * @param position 底部标签的下标
     */
    public void hideNotify(int position){
        mItemViews.get(position).hideNotify();
    }

C
chaychan 已提交
177 178 179 180
    public int getCurrentItem() {
        return mCurrentItem;
    }

C
chaychan 已提交
181 182
    public void setSmoothScroll(boolean smoothScroll) {
        this.mSmoothScroll = smoothScroll;
C
chaychan 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    }

    public BottomBarItem getBottomItem(int position){
        return mItemViews.get(position);
    }

    /**
     * @return 当View被销毁的时候,保存数据
     */
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState());
        bundle.putInt(STATE_ITEM, mCurrentItem);
        return bundle;
    }

    /**
     * @param state 用于恢复数据使用
     */
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            mCurrentItem = bundle.getInt(STATE_ITEM);
            //重置所有按钮状态
            resetState();
            //恢复点击的条目颜色
            mItemViews.get(mCurrentItem).setStatus(true);
            super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE));
        } else {
            super.onRestoreInstanceState(state);
        }
    }

    private OnItemSelectedListener onItemSelectedListener;

    public interface OnItemSelectedListener {
        void onItemSelected(BottomBarItem bottomBarItem, int position);
    }

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }
}