How to Change the viewType of a RecyclerView item onClick

半腔热情 提交于 2019-12-01 16:59:50

see my example:

 // RecycleItemObject.java
 public class RecycleItemObject {
       public static int LAYOUT_ITEM_PORTRAIN = 0;
       public static int LAYOUT_ITEM_LANDSCAPE = 1;
       private int type;
         ....................
      /**
      * @return get layout type of item
      */
      public int getType() {
        return type;
      }

      /**
      * set layout type of item
      * @param type
      */
      public void setType(int type) {
         this.type = type;
      }
  }

 // adapter
 public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == RecycleItemObject.LAYOUT_ITEM_PORTRAIN) {
        view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main, parent, false);
    } else {
        view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main_land, parent, false);
    }
 }

// event click item
@onClick....
curentObject.setType(RecycleItemObject.LAYOUT_ITEM_LANDSCAPE);
// notify item only
notifyItemChanged(itemPosition);

with me it work fine

Hope you have figured out your problem. If not then,I have done it f or you. The buggy line is

 @Override
    public void onClick(View v) {
        itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
    } 

Here what you are doing is adding a new Item to

 itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);

This will insert a new item and size of mdataList and itemClickState differs and hence mis matching happens.

Use itemClickedState.set(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT); instead. And second thing I does not understand what tvTicketClass is . If it is a single TextView, then you should concatenate the string first and them do tvTicketClass.settext(concatenatedstrClass) . Hope you get helped from this post.If have any further issue , please do comment.

override getItemViewType in adapter and return itemtype you want. for more check out this link https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!