25

フラグメント内でジェスチャを利用しようとしています。私の詳細フラグメントを処理するFragmentActivityの中に次のものがあります。私がやろうとしているのは、ビューでスワイプが検出され、そのビュー内のデータが前のエントリまたは次のエントリに置き換えられたときです。

これを処理するより良い方法がある場合; 私はそれですべてです。ただし、ここで起こっていることは、onFlingメソッドが実際に呼び出されることはないということです。

public static class DetailsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = inflater.inflate(R.layout.my_view, null, false);
        final GestureDetector gesture = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {

                    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(Constants.APP_TAG, "Right to Left");
                        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            Log.i(Constants.APP_TAG, "Left to Right");
                            titles.showDetails(getPosition() - 1);
                        }
                    } catch (Exception e) {
                        // nothing
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });

        return v;
    }
}
4

2 に答える 2

46

次の問題がこれを説明しているように見えます:Android:GestureDetectorはGesturesをキャッチしません

さらに、結果は次のとおりです。

解決策は、実際にはonDownメソッドをオーバーライドしてtrueを返すことです。そうしないと、ジェスチャ検出器が停止し、アップを検出しません。

        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) {
                    Log.i(Constants.APP_TAG, "onFling has been called!");
                    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(Constants.APP_TAG, "Right to Left");
                        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            Log.i(Constants.APP_TAG, "Left to Right");
                        }
                    } catch (Exception e) {
                        // nothing
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });
于 2012-07-10T20:39:07.853 に答える
8

いくつかのコメント

  1. 次のようにコードを微調整する必要がありました。

    v.setOnTouchListener(new View.OnTouchListener() {
        @Override        
        public boolean onTouch(View v, MotionEvent event) {
    
            // return gesture.onTouchEvent(event);
    
            gesture.onTouchEvent(event);
            return true; // <-- this line made the difference
        }
    });
    
  2. また、xmlファイルを使用してビューを作成している場合

    View v = inflater.inflate(R.layout.my_view, null, false);
    

実際に目的のビューを押していることを確認してください。テストを誇張する良い方法は、レイアウトxmlファイルで幅と高さの両方を「wrap_content」ではなく「match_parent」にすることです。

于 2016-08-12T04:08:43.700 に答える