我试图禁止ViewPager在选项卡之间滚动。我有以下情况:
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;
}
}
我通常具有onPageScrolled和onPageScrollStateChanged的日志,但没有onInterceptTouchEvent和onTouch的日志。我究竟做错了什么?
最佳答案
这就是我(整个类(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; }
}
bool
paging
为您提供了根据需要打开和关闭此选项的选项。如果不需要,只需return false
。如果您没有在onTouch
中做任何特别的事情,则无需覆盖它。