Skip to content Skip to sidebar Skip to footer

Moving A Recyclerview Via Touch Or Gesture Recognition

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 an

Solution 1:

Move Below Code in One class. {

publicclassOnSwipeTouchListenerimplementsView.OnTouchListener {

    private final GestureDetector gestureDetector;

    publicOnSwipeTouchListener (Context ctx){
        gestureDetector = newGestureDetector(ctx, newGestureListener());
    }

    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final classGestureListenerextendsGestureDetector.SimpleOnGestureListener {

        privatestatic final int SWIPE_THRESHOLD = 100;
        privatestatic final int SWIPE_VELOCITY_THRESHOLD = 100;

        @OverridepublicbooleanonDown(MotionEvent e) {
            returntrue;
        }
        @OverridepublicbooleanonSingleTapUp(MotionEvent e) {
            onClick();
            returnsuper.onSingleTapUp(e);
        }

        @OverridepublicbooleanonDoubleTap(MotionEvent e) {
            onDoubleClick();
            returnsuper.onDoubleTap(e);
        }

        @OverridepublicvoidonLongPress(MotionEvent e) {
            onLongClick();
            super.onLongPress(e);
        }

        @OverridepublicbooleanonFling(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;
                }
                elseif (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;
        }
    }

    publicvoidonSwipeRight() {
    }

    publicvoidonSwipeLeft() {
    }

    publicvoidonSwipeTop() {
    }

    publicvoidonSwipeBottom() {
    }

    publicvoidonClick() {

    }

    publicvoidonDoubleClick() {

    }

    publicvoidonLongClick() {

    }
}

}

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

holder.relMainOfRow.setOnTouchListener(newOnSwipeTouchListener(getActivity()) {

                @OverridepublicvoidonClick() {
                    super.onClick();

                }

                @OverridepublicvoidonLongClick() {
                    super.onLongClick();


                }



                publicvoidonSwipeTop() {

                }

                publicvoidonSwipeRight() {

                }

                publicvoidonSwipeLeft() {

                }

                publicvoidonSwipeBottom() {

                }
            });

}

Post a Comment for "Moving A Recyclerview Via Touch Or Gesture Recognition"