2013-05-02 72 views
22

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

讓我們假設我有一個可拖動的觀點,我需要禁用swipping拖動開始時和重新啓用當拖動完成所以的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; 
} 

回答

32

我想到的,我是有一個習俗,其中,ViewPager當你觸摸的聽衆得到通知的特定事件,你可以設置這第一件事情swipeableboolean in ViewPager爲false並將其重新設置爲t無論哪種方式最適合您的應用程序。

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是其中延伸FragmentActivity並實現ActionBar.TabListener

+0

很好的解決方案。你測試過了嗎?它工作? – 2013-05-02 17:33:43

+0

@asok如何從不同的片段訪問customviewpager實例(在活動中創建)? – 2013-05-02 17:59:18

+0

@Asok不幸的是,沒有任何UI元素會對觸摸做出反應,甚至在使用customviewpager而不是viewpager之後的按鈕和列表視圖。 – 2013-05-02 18:12:17

27

我用我MainActivityrequestDisallowInterceptTouchEvent(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); 
} 
+0

你救我的日子。謝謝:) – 2015-04-02 12:32:22

+0

正是我在找的!謝謝! – spinalfrontier 2016-03-24 02:26:05

+0

輝煌!奇蹟般有效。 – Heisenberg 2016-07-01 16:24:28

0

我使用這樣的

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

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