Tuesday, February 28, 2017

Dynamically adding Views in a RecyclerView only to current item

Leave a Comment

I'm dynamically adding Views to my items in a RecyclerView. These added Views should only be related to the item which they're added to, but I'm having a problem when I scroll. It seems the View is recycled and a new item is loaded, but those previously added views are still there, only now on the wrong item.

I'm assuming that it's just because the ViewHolder is being reused, so the added items show up again with a new item, when loaded.

How would one go about solving this?

10 Answers

Answers 1

You need to track what views have been added based on the backing data. I would probably add any necessary extra views in onBindViewHolder(), and remove any that might be present in onViewRecycled(). Then when you want to make one appear dynamically, change whatever variable you have tracking whether it should be visible, and call notifyItemChanged().

Answers 2

Based on this:

but those previously added Views are still there, but now on the wrong item.

Basically, as per the RecyclerView documentation, You have to reset the views everytime inside the onBindViewHolder() method,

so let say, you have a method that sets a view param if its your profile, so the code for the same goes as follows,

if (list.get(position).getId()==PreferenceManager.getUserID()) {   // do some view change here setViewParam(true); }else {   // reset the view change here setViewParam(false); } 

So what you're doing here is giving recycled ViewHolder a chance to reset. Do comment if you need help!

Answers 3

You can use this! setItemViewCacheSize(int size) Check here RecyclerViewDocumentation.

The offscreen view cache stays aware of changes in the attached adapter, allowing a LayoutManager to reuse those views unmodified without needing to return to the adapter to rebind them.

Answers 4

Save Information by tags for items with new child each time the Add newView operation occur. (In shared preference for example) Tag: create with item position onBindViewHolder.

... SharedPreference  sharedPref = getSharedPreference("text" + position, context); SharedPreference.Editor editor = sharedPref.edit(); editor.putString("view", "ImageView"); ... 

when load Adapter get this value and put default as null. I am not sure about its efficiency but i will work.

... String viewType = sharedPref.getString("view", null); //it will return ImageView  

if you know some possible viewTypes for example always going to be ImageView & TextView so with some if statement it will be ok.

if(viewType.equals("ImageVIew")){ item(position).addView(new ImageVIew(context)); } 

Good Luck

Answers 5

In your adapter class of your recyclerView, in the onBindViewHolder method, create another adapter and do the same methods for your new adapter.

The hierarchy will be,

mainRecyclerView -> item1(->childRecyclerView1) , item2(->childRecyclerView2), item3(->childRecyclerView3)

This way you can achieve what you want without wrong values to be viewed on wrong items.

Answers 6

You should take any Empty Layout like Linearlayout in your child item layout XML and then add views into that LinearLayout of your particular item in this way when you scroll List all of you child views which you have added to LinearLayout also scroll with that item .

Answers 7

First of all, can you share some more code please?

Second, why would you want to dynamically add new views on fly? Why don't you use different VIEWTYPE or just have those view already on your layout and just make them visible/invisible or visible/gone? (I believe it will be more efficient this way).

Let me remind you something about RecyclerView, yes when user is scrolling viewHolder are being reused (few of them can be created, even more than it needs to fill the screen). So if it happened that you added some views on "item A" and user scroll to "item Z", that viewHolder can be reused for that "item Z", hence the show up of the previously added views.

How can you solve that?

Well always check on every items if you need to add new views, if yes add them if not already added, else always remove those views (if present) to return to default viewHolder state (or whatever you call it).

Hope this will help you.

Answers 8

This was an old question of mine. A bounty was placed on it, hence the surge of popularity and the multiple new and irrelevant answers.

As stated in both my comment to this answer and @CQM's comment below my original question, the answer is to override the onViewRecycled() method and perform any needed operations there. This method is called when a view is recycled, and any cleanup operations can be done here.

Documentation on this method can be found here.

In my case, it was a matter of deleting the invisible TextView's attached to the view. The text itself had been deleted, but the view remained. If many invisible TextView's accumulate on the view and aren't properly recycled when scrolling, the scroll will begin to lag.

Answers 9

After adding every view you should call to 'notifyDataSetChanged()' to notify the changes and update the items.

Add your code for a better response

Answers 10

If you want to dynamically add change the views based on your data, recyclerview provides getItemViewType() function. and you can add like below ..

public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.MainViewHolder> {  public static final int TYPE_YOU = 1; public static final int TYPE_ME = 2; private List<ConversationModel> listItems;  public ConversationAdapter(List<ConversationModel> listItems) {     this.listItems = listItems; }  @Override public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      switch (viewType) {         case TYPE_YOU:             ChatYouBinding youBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_you, parent, false);             return new MyViewHolderHeader(youBinding);         case TYPE_ME:             ChatMeBinding meBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_me, parent, false);             return new MyViewHolderContent(meBinding);     }     ChatMeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.chat_me, parent, false);     return new MyViewHolderContent(binding);  }  @Override public void onBindViewHolder(MainViewHolder holder, int position) {     ConversationModel conversationModel = listItems.get(position);       switch (holder.getItemViewType()) {         case TYPE_YOU:             MyViewHolderHeader headerHolder = (MyViewHolderHeader) holder;             headerHolder.chatYouBinding.setConversationyou(conversationModel);             break;         case TYPE_ME:             MyViewHolderContent contentHolder = (MyViewHolderContent) holder;             contentHolder.chatMeBinding.setConversation(conversationModel);              break;     }  }  @Override public int getItemCount() {     return listItems.size(); }   @Override public int getItemViewType(int position) {     return listItems.get(position).getMessageType().equals(Constants.ME) ? TYPE_ME : TYPE_YOU; }  class MainViewHolder extends RecyclerView.ViewHolder {     private MainViewHolder(View v) {         super(v);     } }  private class MyViewHolderContent extends MainViewHolder {     TextView cstmTxtChatUserName, cstmTxtChatMessage, cstmTxtChatSentTime;     ChatMeBinding chatMeBinding;      MyViewHolderContent(ChatMeBinding chatMeBinding) {         super(chatMeBinding.getRoot());         View view = chatMeBinding.getRoot();         this.cstmTxtChatUserName = (TextView) view.findViewById(R.id.cstmTxtChatUserName);         this.cstmTxtChatMessage = (CustomTextView) view.findViewById(R.id.cstmTxtChatMessage);         this.cstmTxtChatSentTime = (CustomTextView) view.findViewById(R.id.cstmTxtChatSentTime);         this.chatMeBinding = chatMeBinding;     } }  private class MyViewHolderHeader extends MainViewHolder {     TextView cstmTxtChatUserName, cstmTxtChatMessage, cstmTxtChatSentTime;     ChatYouBinding chatYouBinding;      MyViewHolderHeader(ChatYouBinding chatYouBinding) {         super(chatYouBinding.getRoot());         View view = chatYouBinding.getRoot();         this.cstmTxtChatUserName = (TextView) view.findViewById(R.id.cstmTxtChatUserName);         this.cstmTxtChatMessage = (CustomTextView) view.findViewById(R.id.cstmTxtChatMessage);         this.cstmTxtChatSentTime = (CustomTextView) view.findViewById(R.id.cstmTxtChatSentTime);         this.chatYouBinding = chatYouBinding;     } }   } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment