Androidで左右スワイプを行いたいです。FragmentのonCreateViewに以下を記述しています。
(Android Studio 3.3.1を利用しています。)
Fragment内は複数のimageViewで画面が覆われていて背後のrootViewのスワイプを認識してくれないように見えてます。
また、imageViewにはOnclickを設定しています。
imageViewを一部、無くして動作を確認すると動いています。
このようなimageViewで満たされたFragment内の画面全体に対して、スワイプを認識させるにはどうすれば良いでしょうか?
できれば、FragmentよりActivityの全体で左右のスワイプを認識したいのですが、同様にGestureDetectorを認識してくれませんので、背後?か画面全体でも認識できる方法をお願いします。
ちなみに、上下のViewPagerでFragment画面を切り替えているのですが、上下スワイプは認識しています。
rootView = inflater.inflate(frag_tab1, container, false); final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final int SWIPE_MIN_DISTANCE = 20; final int SWIPE_MAX_OFF_PATH = 100; final int SWIPE_THRESHOLD_VELOCITY = 200; try { // 左右スワイプ処理用 if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {// Y軸の移動距離が大きすぎる場合 // 何もしない } // 開始位置から終了位置の移動距離が指定値より大きい// X軸の移動速度が指定値より大きい else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { textView2.setText("右から左"); } // 終了位置から開始位置の移動距離が指定値より大きい// X軸の移動速度が指定値より大きい else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { textView2.setText("左から右"); } } catch (Exception e) { // nothing } return super.onFling(e1, e2, velocityX, velocityY); } }); rootView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gesture.onTouchEvent(event); } }); // Return処理 return rootView;
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。