Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to disable Swipe left and up in RecyclerView?

Posted on November 11, 2022 By

Solution 1 :

We can define swipe directions by using SimpleCallback().

For RIGHT:

ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.RIGHT, this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

For LEFT:

ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

For Both:

ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

Problem :

I want to perform only the right swipe on RecyclerView.
But it swipes on both sides and also an upside.
How to disable swipe left and up?
Want to perform delete functionality on the right swipe.

It works but it swipes all the side of items. How to prevent this?

NotificationFragment.java

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
        if (viewHolder instanceof NotificationListAdapter.ViewHolder) {
            mAdapter.removeItem(viewHolder.getAdapterPosition());
         }
    }  

NotificationListAdapter.java

    public void removeItem(int position) {
        callDeleteService(position);
    }  

RecyclerItemTouchHelper

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {

    private RecyclerItemTouchHelperListener listener;

    public RecyclerItemTouchHelper(int dragDirs ,int swipeDirs ,RecyclerItemTouchHelperListener listener) {
        super( dragDirs ,swipeDirs );
        this.listener = listener;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder ,RecyclerView.ViewHolder target) {
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder ,int actionState) {
        if (viewHolder != null) {
            final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;

            getDefaultUIUtil().onSelected( foregroundView );
        }
    }

    @Override
    public void onChildDrawOver(Canvas c ,RecyclerView recyclerView ,
                                RecyclerView.ViewHolder viewHolder ,float dX ,float dY ,
                                int actionState ,boolean isCurrentlyActive) {
        final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
        getDefaultUIUtil().onDrawOver( c ,recyclerView ,foregroundView ,dX ,dY ,
                actionState ,isCurrentlyActive );
    }

    @Override
    public void clearView(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder) {
        final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
        getDefaultUIUtil().clearView( foregroundView );
    }

    @Override
    public void onChildDraw(Canvas c ,RecyclerView recyclerView ,
                            RecyclerView.ViewHolder viewHolder ,float dX ,float dY ,
                            int actionState ,boolean isCurrentlyActive) {
        final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;

        getDefaultUIUtil().onDraw( c ,recyclerView ,foregroundView ,dX ,dY ,
                actionState ,isCurrentlyActive );
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder ,int direction) {
//        if (direction == ItemTouchHelper.END){
        listener.onSwiped( viewHolder ,direction ,viewHolder.getAdapterPosition() );
//        }
//        else {
//            Log.d( "Swipe LEFT  : ", " DO NOTHING" );
//        }

    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder) {
        int dragFlags = 0;
        int swipeFlags = ItemTouchHelper.LEFT;

        return makeMovementFlags( dragFlags ,swipeFlags );
    }

    @Override
    public int convertToAbsoluteDirection(int flags ,int layoutDirection) {
        return super.convertToAbsoluteDirection( flags ,layoutDirection );
    }

    public interface RecyclerItemTouchHelperListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder ,int direction ,int position);
    }
}     

enter image description here enter image description here

I want it like a 1st image.

READ  [FIXED] android - Unreachable Statement while Uploading 2 Images
Powered by Inline Related Posts
Android Tags:android, android-recyclerview, swipe

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] android java notification tap reset activity Android
[FIXED] How do I load React Native Project in Android Studio with an emulator? Android
[FIXED] android studio – Java: why does Calendral.setTimeInMillis(0) set incorrect time Android
[FIXED] android – when assembling apk, it gives an error antivirus trojan on xiaomi phone Android
[FIXED] android – How to handle invalid pthread_t passed to libc? Android
[FIXED] c# – Xamarin Forms ListView stays empty after filling the source Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme