提交 b608de9a 编写于 作者: C chaychan

修改子条目点击的回调方法,增加当前子条目的返回

上级 1901c037
...@@ -24,7 +24,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -24,7 +24,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
private ViewPager mViewPager; private ViewPager mViewPager;
private int mChildCount;//子条目个数 private int mChildCount;//子条目个数
private List<BottomBarItem> itemViews = new ArrayList<>(); private List<BottomBarItem> mItemViews = new ArrayList<>();
private int mCurrentItem;//当前条目的索引 private int mCurrentItem;//当前条目的索引
private boolean mSmoothScroll; private boolean mSmoothScroll;
...@@ -67,7 +67,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -67,7 +67,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
for (int i = 0; i < mChildCount; i++) { for (int i = 0; i < mChildCount; i++) {
if (getChildAt(i) instanceof BottomBarItem) { if (getChildAt(i) instanceof BottomBarItem) {
BottomBarItem bottomBarItem = (BottomBarItem) getChildAt(i); BottomBarItem bottomBarItem = (BottomBarItem) getChildAt(i);
itemViews.add(bottomBarItem); mItemViews.add(bottomBarItem);
//设置点击监听 //设置点击监听
bottomBarItem.setOnClickListener(new MyOnClickListener(i)); bottomBarItem.setOnClickListener(new MyOnClickListener(i));
} else { } else {
...@@ -75,7 +75,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -75,7 +75,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
} }
} }
itemViews.get(mCurrentItem).setStatus(true);//设置选中项 mItemViews.get(mCurrentItem).setStatus(true);//设置选中项
mViewPager.setOnPageChangeListener(this); mViewPager.setOnPageChangeListener(this);
} }
...@@ -88,7 +88,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -88,7 +88,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
public void onPageSelected(int position) { public void onPageSelected(int position) {
mCurrentItem = position;//记录当前位置 mCurrentItem = position;//记录当前位置
resetState(); resetState();
itemViews.get(position).setStatus(true); mItemViews.get(position).setStatus(true);
mViewPager.setCurrentItem(position, mSmoothScroll); mViewPager.setCurrentItem(position, mSmoothScroll);
} }
...@@ -109,12 +109,12 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -109,12 +109,12 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
public void onClick(View v) { public void onClick(View v) {
//回调点击的位置 //回调点击的位置
if (onItemSelectedListener != null) { if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(currentIndex); onItemSelectedListener.onItemSelected(getBottomItem(currentIndex),currentIndex);
} }
//点击前先重置所有按钮的状态 //点击前先重置所有按钮的状态
resetState(); resetState();
itemViews.get(currentIndex).setStatus(true);//设置为选中状态 mItemViews.get(currentIndex).setStatus(true);//设置为选中状态
//不能使用平滑滚动,否者颜色改变会乱 //不能使用平滑滚动,否者颜色改变会乱
mViewPager.setCurrentItem(currentIndex, false); mViewPager.setCurrentItem(currentIndex, false);
//点击是保存当前按钮索引 //点击是保存当前按钮索引
...@@ -127,7 +127,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -127,7 +127,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
*/ */
private void resetState() { private void resetState() {
for (int i = 0; i < mChildCount; i++) { for (int i = 0; i < mChildCount; i++) {
itemViews.get(i).setStatus(false); mItemViews.get(i).setStatus(false);
} }
} }
...@@ -144,6 +144,10 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -144,6 +144,10 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
this.mSmoothScroll = mSmoothScroll; this.mSmoothScroll = mSmoothScroll;
} }
public BottomBarItem getBottomItem(int position){
return mItemViews.get(position);
}
/** /**
* @return 当View被销毁的时候,保存数据 * @return 当View被销毁的时候,保存数据
*/ */
...@@ -166,7 +170,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -166,7 +170,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
//重置所有按钮状态 //重置所有按钮状态
resetState(); resetState();
//恢复点击的条目颜色 //恢复点击的条目颜色
itemViews.get(mCurrentItem).setStatus(true); mItemViews.get(mCurrentItem).setStatus(true);
super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE)); super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE));
} else { } else {
super.onRestoreInstanceState(state); super.onRestoreInstanceState(state);
...@@ -176,7 +180,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha ...@@ -176,7 +180,7 @@ public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageCha
private OnItemSelectedListener onItemSelectedListener; private OnItemSelectedListener onItemSelectedListener;
public interface OnItemSelectedListener { public interface OnItemSelectedListener {
void onItemSelected(int position); void onItemSelected(BottomBarItem bottomBarItem,int position);
} }
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) { public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册