简体   繁体   English

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

[英]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:我正在尝试将 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;
    }
}

And I attach it to recyclerView in this code:我在这段代码中将它附加到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
    }
}

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 ".但是,即使我的 logcat 打印了有关方法“ getMovementFlags ”和“ isItemViewSwipeEnabled ”的信息,也没有滑动 animation,也没有方法“ onSwiped ”的 logcat 信息。 Here is the logcat info:这是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: 

I can't figure out what happened.我无法弄清楚发生了什么。 Please help me.请帮我。

You should use makeMovementFlags instead of using makeFlags inside getMovementFlags.您应该使用makeMovementFlags而不是在 getMovementFlags 中使用makeFlags

You can also use a library I made to do this action.您也可以使用我制作的库来执行此操作。

Here is the link这是链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用RecyclerView.VERTICAL而不是LinearLayoutManager.VERTICAL吗? - Should I use RecyclerView.VERTICAL instead of LinearLayoutManager.VERTICAL? 未解决的参考:LinearLayoutManager/RecyclerView - Unresolved reference: LinearLayoutManager/RecyclerView 垂直LinearLayoutManager崩溃的应用程序 - Vertical LinearLayoutManager Crashes App 使用LinearLayoutManager集中化RecyclerView - Centralizing RecyclerView with LinearLayoutManager 使用 LinearLayoutManager 在 RecyclerView 中滚动到顶部 - Scroll to top in RecyclerView with LinearLayoutManager 带有GridLayoutManager的RecyclerView和带有LinearLayoutManager的RecyclerView - RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager 禁止水平回收视图收听垂直滑动手势 - Disallow horizontal recyclerview to listen to vertical swipe gesture xamarin RecyclerView LinearLayoutManager StackFromEnd丢失 - xamarin RecyclerView LinearLayoutManager StackFromEnd missing RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working RecyclerView 将 MaterialToggleButtonGroup 添加到父 LinearLayoutManager - RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM