27

I have a viewpager which switches between tabs when swiping left/right.In my second tab, i have some custom views which have listeners for pinching and dragging but when i try to pinch or drag, the viewpager starts to swipe the page.

A solution comes to my mind is to disable swiping when touching those specific views and only swipe when touching outside those views.Is this possible?

Updated: @Asok kindly provided the solution. But then updated the code which wouldnt work in my case so i post the previous piece of code which worked for me:

public class CustomViewPager extends ViewPager {
private boolean swipeable = true;

public CustomViewPager(Context context) {
    super(context);
}

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
    this.swipeable = swipeable;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
    return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}

Lets suppose i have a draggable view and i need to disable swipping when dragging start and re enable when dragging finished so in TouchEvent of my so called view:

    @Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
//disable swiping when the button is touched
((ActivityOriginal) getActivity()).setSwipeable(false);
//the rest of the code...
        break;
    case MotionEvent.ACTION_MOVE:

        break;
    case MotionEvent.ACTION_UP:
//re enable swipping when the touch is stopped
//the rest of the code...
((ActivityOriginal) getActivity()).setSwipeable(true);
        break;
    }
    return true;
}
4

4 回答 4

35

我想到的第一件事是有一个自定义ViewPager,当您的触摸侦听器收到特定事件的通知时,您可以将swipeable booleanin设置ViewPager为 false 并将其设置回 true,无论哪种方式最适合您的应用程序。

public class CustomViewPager extends ViewPager {
    private boolean swipeable = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Call this method in your motion events when you want to disable or enable
    // It should work as desired.
    public void setSwipeable(boolean swipeable) {
        this.swipeable = swipeable;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false; 
    }

}

确保更改布局文件以显示:

<com.your.package.CustomViewPager .. />

代替:

<android.support.v4.view.ViewPager .. />

编辑 2

这是我的设置(使用上述设置CustomViewPager):

CustomViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the CustomViewPager with the sections adapter.
    mViewPager = (CustomViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

}

public void swipeOn(View v) {
    mViewPager.setSwipeable(true);
}

public void swipeOff(View v) {
    mViewPager.setSwipeable(false);
}

上面显示onCreate的是在我的MainActivity类中扩展FragmentActivity 和实现ActionBar.TabListener

于 2013-05-02T16:31:36.733 回答
35

我正在使用requestDisallowInterceptTouchEvent(true) int 也具有拖动事件的视图的 onTouchEvent 侦听器。

@Override
public boolean onTouchEvent(MotionEvent event) {

    ViewParent parent = getParent(); 
    // or get a reference to the ViewPager and cast it to ViewParent

    parent.requestDisallowInterceptTouchEvent(true);

    // let this view deal with the event or
    return super.onTouchEvent(event);
}
于 2013-10-15T14:39:07.720 回答
0

我正在这样使用

public void setSwipeable(boolean swipeable)
{
    this.swipeable = swipeable;
}

@Override
public void scrollTo(int x, int y){
    if (swipeable){
        super.scrollTo(x, y);
    }
}
于 2014-07-31T08:31:37.837 回答
0

如果您知道不想在视图寻呼机中拦截触摸事件的位置,您可以像我一样做一些事情。

 @Override
 public boolean onInterceptTouchEvent(MotionEvent arg0) {
    if(arg0.getY()>getHeight()/2)return false;
    return super.onInterceptTouchEvent(arg0);
}
于 2019-01-27T12:47:21.550 回答