簡體   English   中英

如何在觸摸特定視圖時禁用viewpager適配器?

[英]how to disable viewpager adapter on touching specific views?

我有一個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;
}

我想到的,我這第一件事情就是有一個自定義ViewPager中,當你觸摸的聽眾得到通知的特定事件,你可以設置swipeable booleanViewPager為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

我正在使用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);
}

我這樣用

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

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

如果您知道您不想在視圖尋呼機中攔截觸摸事件的位置,您可以像我一樣做。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM