ItemToucherHelper not swipe in recyclerView with vertical LinearLayoutManager

I am trying to attach an ItemTouchHelper to my recyclerView to implement swipe-to-read functionality, I have this code for ItemTouchHelper.Callback:

public class MessageCommentSwipeCallback extends ItemTouchHelper.Callback {
    private static final String TAG="MessageSwipeCallback";
    private final MessageCommentAdapter adapter;

    public MessageCommentSwipeCallback(MessageCommentAdapter adapter) {
        this.adapter = adapter;
        Log.i(TAG, "MessageCommentSwipeCallback: ");
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        Log.i(TAG, "getMovementFlags: ");
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;        //允许上下的拖动
        int swipeFlags = ItemTouchHelper.LEFT;   //只允许从右向左侧滑
        return makeFlag(dragFlags,swipeFlags);
    }

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

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        Log.i(TAG, "onSwiped: ");
        adapter.onMessageRead(viewHolder.getAbsoluteAdapterPosition());
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        Log.i(TAG, "isItemViewSwipeEnabled: ");
        return true;
    }
}

And I attach it to recyclerView in this code:

public class MessageCommentFragment extends Fragment {

    private static final String TAG = "MessageCommentFragment";
    private FragmentMessageCommentBinding binding;
    private MessageCommentViewModel messageCommentViewModel;
    private MessageCommentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMessageCommentBinding.inflate(inflater);
        //here omit unimportant code
        binding.recyclerViewMessageComment.setLayoutManager(new LinearLayoutManager(getContext()));
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //为recyclerView添加左滑监听
        MessageCommentSwipeCallback callback=new MessageCommentSwipeCallback(adapter);
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(binding.recyclerViewMessageComment);
        //here omit unimportant code
    }
}

However, even though my logcat print the info about the methods "getMovementFlags" and "isItemViewSwipeEnabled", there is no swiping animation, as well there is no logcat info for method "onSwiped". Here is the logcat info:

2021-04-01 11:14:55.922 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: isItemViewSwipeEnabled: 
2021-04-01 11:14:55.939 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: isItemViewSwipeEnabled: 
2021-04-01 11:14:55.939 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: getMovementFlags: 
2021-04-01 11:14:55.955 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: isItemViewSwipeEnabled: 
2021-04-01 11:14:55.955 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: getMovementFlags: 
2021-04-01 11:14:55.971 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: isItemViewSwipeEnabled: 
2021-04-01 11:14:55.971 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: getMovementFlags: 
2021-04-01 11:14:55.988 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: isItemViewSwipeEnabled: 
2021-04-01 11:14:55.988 15994-15994/com.cztcode.dailymoments I/MessageSwipeCallback: getMovementFlags: 

I can’t figure out what happened. Please help me.

Source: Android Questions

LEAVE A COMMENT