前言:mmp网上搜的千篇一律,而且不好使!!!~~~坑啊。。
哎!!:亲自踩坑!
接下来实现:拦截tablayout 点击事件,不触发viewpaper滚动。。。
步骤如下:
前言:
# 涉及:
TabLayout segeView;
KapCustomViewPager hScrollerView;// 可以禁止滚动的 ViewPager
KapMobileCheckView mobileCheckView = null;// 手机号码验证自定义view
KapEmailCheckView emailCheckView = null;// 邮箱验证自定义view
# 滚动绑定:
segeView.setupWithViewPager(hScrollerView);
mobileCheckView = new KapMobileCheckView(this,null);
emailCheckView = new KapEmailCheckView(this,null);
ArrayList<View> arrayList = new ArrayList<View>(){{
add(mobileCheckView);
add(emailCheckView);
}};
hScrollerView.setAdapter(new QuickPageAdapter<View>(arrayList) {
@Override
public CharSequence pageTitle(int position) {
String titles[]= {"手机号码验证","邮箱验证"};
return titles[position];
}
});
- 第一步:禁止ViewPager的滚动
hScrollerView.setCanScroll(false);
/**
* Created by jing on 2018/5/4.
* 可以禁止滚动的 ViewPager
*/
public class KapCustomViewPager extends ViewPager {
private boolean isCanScroll = true;
public KapCustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
//----------禁止左右滑动------------------
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isCanScroll) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (isCanScroll) {
return super.onInterceptTouchEvent(arg0);
} else {
return false;
}
}
public void setCanScroll(boolean canScroll) {
isCanScroll = canScroll;
}
public boolean isCanScroll() {
return isCanScroll;
}
}
- 拦截tablayout 点击事件:
/** 拦截tablayout点击事件 */
View.OnTouchListener tabOnClickListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int pos = (int) view.getTag();
/** 下面都是自定义逻辑了 */
if (pos==0 && StringDetectionHelper.isBlackString(userDetail.getMobile())) {
KapHUDManager.errorWithTitle("未绑定手机号");
return true; // 拦截
}
if (pos==1 && StringDetectionHelper.isBlackString(userDetail.getEmail())) {
KapHUDManager.errorWithTitle("未绑定邮箱");
return true; // 拦截
}
return false; // 不拦截
}
};
for (int i=0;i<segeView.getTabCount();i++) {
View view = getTabView(segeView,i);
if (view == null) continue;
view.setTag(i);
view.setOnTouchListener(tabOnClickListener);
}
/** 反射获取tabview */
public View getTabView( TabLayout tabLayout,int index){
TabLayout.Tab tab = tabLayout.getTabAt(index);
if (tab == null) return null;
View tabView = null;
Field view = null;
try {
view = TabLayout.Tab.class.getDeclaredField("mView");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
view.setAccessible(true);
try {
tabView = (View) view.get(tab);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return tabView;
}
完毕~~~
多逼逼一句:
/** 代码设置当前的index */
TabLayout.Tab tab = segeView.getTabAt(0);// 第一个,1234 类推
if (tab == null) return;
tab.select();
~~~ 长路漫漫,唯坑为伴