2012-05-31 20 views
7

在我的項目中,使用onTouchEvent來檢測屏幕上的觸摸並相應地執行操作,並使用viewPager進行滑動操作。問題在於滑動觸摸操作正在執行。所以從here找到解決辦法。但我的新問題是觸摸在TouchEvent.Action_up被執行後被禁用。代碼如下:當使用ViewPager和PassByValue刷寫頁面時,onTouchEvent正在執行

parent.setOnTouchListener(new OnTouchListener() { 

    public boolean onTouch(View v, MotionEvent event) { 

     switch(event.getAction()) 
     { 
     case MotionEvent.ACTION_MOVE: 
       awesomePager.requestDisallowInterceptTouchEvent(true); 
       break; 
     case MotionEvent.ACTION_UP: 
      awesomePager.requestDisallowInterceptTouchEvent(true); 
      if(flag) 
      { 
       upperdock.setClickable(false); 
       upperdock.bringToFront(); 
       tocparent.bringToFront(); 
       tocbottom.bringToFront(); 
       upperdock.setVisibility(RelativeLayout.VISIBLE); 
       tocparent.setVisibility(LinearLayout.VISIBLE); 
       tocbottom.setVisibility(LinearLayout.VISIBLE); 
       flag=false; 
      } 
      else 
      { 

       parent.bringToFront(); 
       upperdock.setVisibility(RelativeLayout.INVISIBLE); 
       tocparent.setVisibility(LinearLayout.INVISIBLE); 
       tocbottom.setVisibility(LinearLayout.INVISIBLE); 
       flag=true; 
      } 
      break; 

     case MotionEvent.ACTION_CANCEL: 
      awesomePager.requestDisallowInterceptTouchEvent(false); 
      break; 
     default: 
      break; 
     } 

     return false; 
    } 
}); 

在上面的代碼,如果我返回是沒有得到執行假Action_up ..如果我返回true Action_cancel是沒有得到executed..that是傳值的問題在那裏。

回答

2

我改變了這個。

不使用 awesomePager.requestDisallowInterceptTouchEvent(false), 只是使用return true

活動定義布爾sroll;

設置sroll = positionOffset!= 0.0ViewPager.onPageScrolled 和我重寫的onTouchEvent,其中在ViewPager視圖

現在你可以檢查如果(event.getAction()== MotionEvent.ACTION_UP & &!sroll),以決定是否將引發

1

我有同樣的問題的情況下!對我來說,這個工作非常出色....

case MotionEvent.ACTION_MOVE: { 
     getParent().requestDisallowInterceptTouchEvent(false); 
     break; 
    } 

case MotionEvent.ACTION_CANCEL:{ 
     getParent().requestDisallowInterceptTouchEvent(true); 
     break; 

} 

case MotionEvent.ACTION_UP:{ 

//delete awesomePager.requestDisallowInterceptTouchEvent(true); 
//you don't need it here! 
       . 
       . 
      do your stuff.... 
       . 

} 

回到你的整個onTouch梅索德爲真實不虛...

return true;