如何在触摸特定视图时禁用viewpager适配器?

时间:2013-05-02 16:01:28

标签: android android-viewpager

我有一个viewpager,可以在向左/向右滑动时在标签之间切换。在我的第二个标签中,我有一些自定义视图,其中包含用于捏合和拖动的侦听器,但是当我尝试捏合或拖动时,viewpager开始刷页面

我想到的解决方案是在触摸这些特定视图时禁用滑动,只有在触摸这些视图时才滑动。这可能吗?

更新 @Asok友好地提供了解决方案。但后来更新了在我的情况下无法工作的代码,所以我发布了一段对我有用的代码:

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;
}

假设我有一个可拖动的视图,我需要在拖动开始时禁用滑动,并在拖动完成时重新启用,所以在我所谓的视图的TouchEvent中:

    @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 个答案:

答案 0 :(得分:35)

我想到的第一件事就是拥有一个自定义ViewPager,当您的触控听众收到特定事件的通知时,您可以设置swipeable boolean ViewPager为false,并将其设置为最适合您应用的最佳方式。

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

答案 1 :(得分:32)

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

@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);
}

答案 2 :(得分:0)

我正在使用这个

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

@Override
public void scrollTo(int x, int y){
    if (swipeable){
        super.scrollTo(x, y);
    }
}

答案 3 :(得分:0)

如果您知道不想在视图寻呼机中拦截触摸事件的位置,则可以执行与我相同的操作。

 @Override
 public boolean onInterceptTouchEvent(MotionEvent arg0) {
    if(arg0.getY()>getHeight()/2)return false;
    return super.onInterceptTouchEvent(arg0);
}