我有一个ImageView
withonTouchListener
里面的ViewPager
. 当然,当我尝试更改' 页面时, ImageView
' 监听器会触发。ViewPager
有没有办法防止它。我想我需要以某种方式从滑动中识别触摸。
这是我的代码的摘录:
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;
}
});