I'm trying to disallow my ViewPager from scrolling between tabs. I have the following situation:
public class MyClass extends ViewPager implements ViewPager.OnPageChangeListener, OnTouchListener {
public MyClass() {
setOnTouchListener(this);
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d("Testing", TAG + " onPageScrollStateChanged");
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d("Testing", TAG + " onPageScrolled");
}
@Override
public void onPageSelected(int position) {
Log.d("Testing", TAG + " onPageSelected");
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
Log.d("Testing", "intercept");
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("Testing", "touch2");
return false;
}
}
I have the logs from onPageScrolled and onPageScrollStateChanged normally, but I do not have logs from onInterceptTouchEvent and onTouch. What am I doing wrong?
This is how I handle this (my entire class) :
public class SelectiveViewPager extends ViewPager {
private boolean paging = true;
public SelectiveViewPager(Context context) {
super(context);
}
public SelectiveViewPager(Context context, AttributeSet attributeSet){
super(context, attributeSet);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
if (paging) {
return super.onInterceptTouchEvent(e);
}
return false;
}
public void setPaging(boolean p){ paging = p; }
}
The boolean paging
gives you the option to turn this on and off, if you need it. If you don't need, just return false
. If you aren't doing anything special in onTouch
, you don't need to override it.
return true;
in your onInterceptTouchEvent();
Your constructor doesn't get called btw. Are you creating the view pager by code or inflating it by xml? You need to define the proper constructors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.