先写一个控制ViewPager是否自由滑动
public class NoScrollViewPager extends ViewPager{
public void setNeedScroll(boolean needScroll) {
isNeedScroll = needScroll;
}
private boolean isNeedScroll=true;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(isNeedScroll){
return super.onTouchEvent(ev);
}else {
return false;
}
}
自由控制TabLayout是否可以点击:
public void setTabLayoutCanClick(boolean canClick){
LinearLayout tabStrip= (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < tabStrip.getChildCount(); i++) {
View tabView = tabStrip.getChildAt(i);
if(tabView !=null){
tabView.setClickable(canClick);
}
}
}