Android的碎片onCreateView用手势用手、碎片、Android、onCreateView

2023-09-03 21:53:26 作者:乖戾

我试图利用片段中的手势;我有我的处理细节片段FragmentActivity以下里面。当一个滑动上检测的观点,以取代该视图与previous或下一个条目内的数据什么我试图有发生的是

如果有处理这更好的办法;我完全赞成。然而,这里发生的一切就是onFling方法不会实际调用。

 公共静态类DetailsFragment扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
        如果(集装箱== NULL){
            返回null;
        }
        视图V = inflater.inflate(R.layout.my_view,空,假);
        最后GestureDetector姿态=新GestureDetector(getActivity()
            新GestureDetector.SimpleOnGestureListener(){
                @覆盖
                公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
                    浮动velocityY){

                    最终诠释SWIPE_MIN_DISTANCE = 120;
                    最终诠释SWIPE_MAX_OFF_PATH = 250;
                    最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
                    尝试 {
                        如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
                            返回false;
                        如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE
                            &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                            Log.i(Constants.APP_TAG,从右至左);
                        }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE
                            &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                            Log.i(Constants.APP_TAG,左到右);
                            titles.showDetails(为getPosition() -  1);
                        }
                    }赶上(例外五){
                        //什么
                    }
                    返回super.onFling(E1,E2,velocityX,velocityY);
                }
            });

        v.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                返回gesture.onTouchEvent(事件);
            }
        });

        返回伏;
    }
}
 

解决方案

看起来像下面的问题说明这一点:Android: GestureDetector不会赶上手势

北邮在线 Android 10以后你不用再羡慕 iPhone 的手势操作了

此外这里是结果:

解决的办法其实重写onDown方法并返回true;否则手势检测器将停止,检测不到了:

 最后GestureDetector姿态=新GestureDetector(getActivity()
            新GestureDetector.SimpleOnGestureListener(){

                @覆盖
                公共布尔onDown(MotionEvent E){
                    返回true;
                }

                @覆盖
                公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
                    浮动velocityY){
                    Log.i(Constants.APP_TAG,onFling被称为!);
                    最终诠释SWIPE_MIN_DISTANCE = 120;
                    最终诠释SWIPE_MAX_OFF_PATH = 250;
                    最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
                    尝试 {
                        如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
                            返回false;
                        如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE
                            &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                            Log.i(Constants.APP_TAG,从右至左);
                        }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE
                            &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                            Log.i(Constants.APP_TAG,左到右);
                        }
                    }赶上(例外五){
                        //什么
                    }
                    返回super.onFling(E1,E2,velocityX,velocityY);
                }
            });

        v.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                返回gesture.onTouchEvent(事件);
            }
        });
 

I'm attempting to utilize Gestures within a fragment; I have the following inside of a FragmentActivity which handles my details fragment. What I am attempting to have happen is when a swipe is detected on the view to replace the data inside of that view with the previous or next entry.

If there is a better way of handling this; I am all for it. However, what is happening here is that the onFling method is never actually called.

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;
    }
}

解决方案

Looks like the following issue explains this: Android: GestureDetector won't catch Gestures

Additionally here is the result:

The solution is actually to Override the onDown method and return true; otherwise the gesture detector will stop and not detect the up:

        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);
            }
        });