Disable Swipe for position in RecyclerView using ItemTouchHelper.SimpleCallback

前端 未结 7 620
面向向阳花
面向向阳花 2021-01-30 02:44

I am using recyclerview 22.2.0 and the helper class ItemTouchHelper.SimpleCallback to enable swipe-to-dismiss option to my list. But as I have a type of header on it, I

相关标签:
7条回答
  • 2021-01-30 03:33

    In addition to the @Rafael Toledo answer, If you want to remove the specific swipe gesture like LEFT swipe or RIGHT swipe based on position or type of data in object in Adapter, then you can do like this.

    @Override
      public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
           {
              int position = viewHolder.getAdapterPosition();
              ConversationDataModel conversationModel = null;
              conversationModel = mHomeAdapter.getItems().get(position);
              boolean activeChat = true;
              if(conversationModel!=null && !conversationModel.isActive())
               {
                 activeChat = false;
               }
      return activeChat ? super.getSwipeDirs(recyclerView, viewHolder): ItemTouchHelper.RIGHT;
            }
    

    Here in my case, In my Recyclerview I am loading the chat conversations and it have RIGHT (for ARCHIVE) & LEFT ( for EXIT) swipe gestures on each item. But in case if the conversation is not active, then I need to disable the RIGHT swipe for the specific item in the Recycler view.

    So I am checking for the activeChat and accordingly and disabled the RIGHT swipe.

    0 讨论(0)
提交回复
热议问题