我有一個ImageView
與onTouchListener
在ViewPager
的內部。當我嘗試更改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;
}
});
,如果你有什麼只需從ImageView的OnTouchListener中返回'false'?這將允許ViewPager接收觸摸事件。 – Sam
ViewPager接收事件的權利。我想要的是ImageView不會嘗試更改頁面。 – slezadav
您是否嘗試過使用['SimpleOnGestureListener'](http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html)來忽略ImageView中的滑動事件? – Sam