ItemToucherHelper 不使用垂直 LinearLayoutManager 在 recyclerView 中滑动

ItemToucherHelper not swipe in recyclerView with vertical LinearLayoutManager

我正在尝试将 ItemTouchHelper 附加到我的 recyclerView 以实现滑动阅读功能,我有 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;
    }
}

我在这段代码中将它附加到 recyclerView:

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

然而,即使我的 logcat 打印了关于方法“getMovementFlags”和“isItemViewSwipeEnabled”的信息,那里没有滑动动画,方法“onSwiped”也没有 logcat 信息。这是 logcat 信息:

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: 

我想不通发生了什么。请帮助我。

您应该在 getMovementFlags 中使用 makeMovementFlags 而不是使用 makeFlags

您也可以使用我制作的库来执行此操作。

这是link