2012-10-25 42 views
2

我有一個ImageViewonTouchListenerViewPager的內部。當我嘗試更改ViewPager的頁面時,當然ImageView的聽衆會關閉。有沒有辦法阻止它。我想我需要以某種方式識別輕掃。在ViewPager中的onTouchListener

這是我的代碼的摘錄:

public class ServerDetailFragment extends Fragment implements SimpleGestureListener { 
GestureDetector detector; 
SimpleOnGestureListener gestures = new SimpleOnGestureListener() { 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     Log.e("TAG", "onFling"); 
     return true; 
    }; 
}; 
.... 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    detector = new GestureDetector(ServerDetailFragment.this.getActivity(), gestures); 

} 

.... 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.fragment_server_detail, container, false); 
    MyPagerAdapter adapter = new MyPagerAdapter(); 
    myPager = (ViewPager) rootView.findViewById(R.id.detail_pager); 
    myPager.setAdapter(adapter); 
    myPager.setCurrentItem(0); 
    myPager.setOffscreenPageLimit(4); 

    ViewTreeObserver vto = myPager.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      pagerGraph = (ImageView) myPager.findViewWithTag(myPager.getCurrentItem()).findViewById(R.id.pagerView); 
      pagerPb = (ProgressBar) myPager.findViewWithTag(myPager.getCurrentItem()).findViewById(R.id.pagerPb); 
      if (init) { 

       plotFakeData(); 

       // Following problematic part is also in ViewPager's onPageChangeListener 

         pagerGraph.setOnTouchListener(new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         if (detector.onTouchEvent(event)) { 

          // some more code here 

         } 
         return false; 
        } 

       }); 

       // myPager.setCurrentItem(0); 
      } 
      init = false; 
     } 
    }); 
+0

,如果你有什麼只需從ImageView的OnTouchListener中返回'false'?這將允許ViewPager接收觸摸事件。 – Sam

+0

ViewPager接收事件的權利。我想要的是ImageView不會嘗試更改頁面。 – slezadav

+0

您是否嘗試過使用['SimpleOnGestureListener'](http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html)來忽略ImageView中的滑動事件? – Sam

回答

5

我會像這樣的東西去:

public class OnSwipeTouchListener implements OnTouchListener { 

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

public boolean onTouch(final View v, final MotionEvent event) { 
    return gestureDetector.onTouchEvent(event); 
} 

private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     onTouch(e); 
     return true; 
    } 


    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
        } else { 
        } 
       } 
      } else { 
       // onTouch(e); 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 
public void onTouch(MotionEvent e) { 
} 
} 

,並呼籲他們這樣說:

pagerGraph.setOnTouchListener(new OnSwipeTouchListener() {   
public void onTouch(MotionEvent event) { 
    // your code here 
}); 
+0

現在不推薦使用GestureDetector構造函數,因此您可以使用活動上下文構造偵聽器,然後在該位置實例化GestureDetector。 –

+0

我相信你是絕對正確的。但看問題是什麼時候問的,當我回答它時:)沒有關於「當答案過期時要做什麼」的書面政策......(http://meta.stackoverflow.com/questions/265749/whats-the -policy上向下投票,以前正確的,但是,現在已經過時,答案)。我很樂意告訴你,如果你想把它添加到我的答案中去 - 但這不是我相信的正確方法。你可以添加它作爲一個全新的答案說 - 從版本4.X.Y ...等。 – Michal

+0

那麼,如果你想對此迂腐,那個構造函數自2009年4月起已被棄用,所以你是對的,我應該創建一個新的答案... –