android - 在带有滚动视图的片段中检测滑动手势
android - 在带有滚动视图的片段中检测滑动手势
我正在实现一个带有滚动视图的片段,并且我想在此片段中检测从左到右的滑动。我使用了以下代码来检测滑动事件:\n
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i("SWIPE", "onFling已被调用!"); final int SWIPE_MIN_DISTANCE = 120; final int SWIPE_MAX_OFF_PATH = 250; final int SWIPE_THRESHOLD_VELOCITY = 200; try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; } if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.i("SWIPE", "从右到左"); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.i("SWIPE", "从左到右"); } } catch (Exception e) { e.printStackTrace(); } return super.onFling(e1, e2, velocityX, velocityY); } }); container.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch (View v, MotionEvent event){ return gesture.onTouchEvent(event); } }); // 为此片段填充布局 return inflater.inflate(R.layout.fragment_blank, container, false); }
\n我已经在没有滚动视图的布局中测试了这段代码,并且它正常工作。但是,一旦我在布局中插入滚动视图,滑动事件就不再被检测到。\n
\n有人能帮忙吗?我将不胜感激。