Updating RecyclerView With A Different ItemViewType

Nathan Sokalski 4,111 Reputation points
2021-05-17T04:49:30.727+00:00

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?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-05-18T07:02:18.31+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.