question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Updating RecyclerView With A Different ItemViewType

I have an Adapter that overrides the GetItemViewType method. There is a point in my code where I remove an item from the data of the Adapter and call NotifyItemRemoved for this. This works fine for the item being removed, but the item that previously followed the removed item needs to be updated, not with different data, but as a different ItemViewType. In other words, I need to explicitly recreate or update it with OnBindViewHolder so that it uses a different ItemViewType. Because the data is not changing, I don't think NotifyItemChanged is doing anything (it seems to look the same). How can I force the Adapter to refresh the item?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 commented

Hello,


Welcome to our Microsoft Q&A platform!

but the item that previously followed the removed item needs to be updated, not with different data, but as a different ItemViewType.

When RecyclerView has multiple ViewHolders, we usually override GetItemViewType method.

  getItemViewType(int position)

This method's default implementation will always return 0, indicating that there is only 1 type of view. In your case, it is not so, and so you will need find a way to assert which row corresponds to which view type.

Besides,when we should notice the viewType parameter of following method:

       public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)

According to the view type, we'll need to inflate the correct layout resource and create our view holder accordingly. The RecyclerView will handle recycling different view types in a way which avoids clashing of different view types.

For example: (assume you can match your viewholders with your object's field Type)

 private  const int LAYOUT_ONE = 0;
 private  const int LAYOUT_TWO = 1;

method GetItemViewType

 public override int GetItemViewType(int position)
 {
     if (items[position].Type == 0)
         return LAYOUT_ONE;
     else
         return LAYOUT_TWO;
 }

method OnCreateViewHolder

 public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
     {
         View view = null;
         switch (viewType)
         {
             case LAYOUT_ONE:
                 view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.customview_user_writepostbar, parent, false);
                 return new CreatePostViewHolder(view);
    
             case LAYOUT_TWO:
                 view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.customview_postregular, parent, false);
                 return new PostRegularViewHolder(view);
    
         }
     }

method OnBindViewHolder

 public override void 
     OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
 {
     int type = GetItemViewType(position);

     switch (type)
     {
         case LAYOUT_ONE:
             CreatePostViewHolder vh2 = holder as CreatePostViewHolder;
             vh2.userFirstName.Text = UserFirstName + ", share something inspiring!";
             break;
         case LAYOUT_TWO:
             PostRegularViewHolder vh = holder as PostRegularViewHolder;
             // other code
             break;
         default:
             break;
     }
 }

So, in short , you can change the Type of your item model in code-behind( items[position].Type ) and refresh the UI. Then it will change to a different ItemViewType.


Best Regards,


Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.



Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @njsokalski , I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·