Moving a RecyclerView via Touch or Gesture Recognition

こ雲淡風輕ζ 提交于 2020-01-11 12:39:29

问题


I am attempting to move and grow, a RecyclerView so that the contents take up the full screen base on touch input. I want the RecyclerView to maintain the ability to scroll left and right.

I was unable to get a GestureDetector to work properly with the RecyclerView. Catching the onScrollChange doesn't work as it may not have the ability to scroll. I attempting onTouchEvent but the results were rather buggy. Does anyone have any advice?

Repo: https://github.com/CubanAzcuy/Animation-Test

        mListView.setOnTouchListener(new View.OnTouchListener() {
        Float mHistoricX = null;
        Float mHistoricY = null;

        Float mHistoricX2 = null;
        Float mHistoricY2 = null;

        int mScrollDirection = 0;
        //1 = Left Right
        //2 = Up Down

        @Override
        public boolean onTouch(View v, MotionEvent e) {
            Log.d("TAG", "eX: " + e.getX() + " eY: " + e.getY());
            switch (e.getAction()) {

                    case MotionEvent.ACTION_UP:
                        Log.d("TAG", "ACTION_UP");

                        mHistoricX = null;
                        mHistoricY = null;
                        mScrollDirection = 0;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d("TAG", "ACTION_MOVE");
                        if(mHistoricX == null || mHistoricY == null) {
                            mHistoricX = e.getX();
                            mHistoricY = e.getY();
                        } else {
                            if(mScrollDirection == 0) {
                                float tempX = Math.abs(mHistoricX - e.getX());
                                float tempy = Math.abs(mHistoricY - e.getY());

                                if(tempX >= tempy) {
                                    mScrollDirection = 1;
                                } else {
                                    mScrollDirection = 2;
                                }

                                mHistoricX2 =  mHistoricX - e.getX();
                                mHistoricY2 = mHistoricY - e.getY();

                            } else {
                                mHistoricX2 =  mHistoricX - e.getX();
                                mHistoricY2 = mHistoricY - e.getY();
                                Log.d("TAG", "X: " + mHistoricX2 + " Y: " + mHistoricY2);

                                mHistoricX = e.getX();
                                mHistoricY = e.getY();
                            }

                        }
                        break;
                    default:
                        break;

                }
            if(mScrollDirection == 2){
                mListView.animate().setDuration(0).xBy(-mHistoricX2).yBy(-mHistoricY2);
                return true;
            }

            return false;
        }
    });

回答1:


Move Below Code in One class. {

public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onClick();
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            onDoubleClick();
            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            onLongClick();
            super.onLongPress(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }

    public void onClick() {

    }

    public void onDoubleClick() {

    }

    public void onLongClick() {

    }
}

}

Use This Listener as Mentioned Below. Set Touch listener to your row file's parent view. {

holder.relMainOfRow.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {

                @Override
                public void onClick() {
                    super.onClick();

                }

                @Override
                public void onLongClick() {
                    super.onLongClick();


                }



                public void onSwipeTop() {

                }

                public void onSwipeRight() {

                }

                public void onSwipeLeft() {

                }

                public void onSwipeBottom() {

                }
            });

}



来源:https://stackoverflow.com/questions/41033279/moving-a-recyclerview-via-touch-or-gesture-recognition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!