I\'m trying to refresh specific item in RecyclerView
.
Story: Whenever user clicks on item, it shows AlertDialog
. User can
Add the changed text to your model data list
mdata.get(position).setSuborderStatusId("5");
mdata.get(position).setSuborderStatus("cancelled");
notifyItemChanged(position);
In your RecyclerView adapter, you should have an ArrayList and also one method addItemsToList(items)
to add list items to the ArrayList. Then you can add list items by call adapter.addItemsToList(items)
dynamically. After all your list items added to the ArrayList then you can call adapter.notifyDataSetChanged()
to display your list.
You can use the notifyDataSetChanged
in the adapter for the RecyclerView
In your adapter class, in onBindViewHolder method, set ViewHolder to setIsRecyclable(false) as in below code.
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder p1, int p2)
{
// TODO: Implement this method
p1.setIsRecyclable(false);
// Then your other codes
}
That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView
/*Firstly, register your new data to your model*/
DataModel detail = new DataModel(id, name, sat, image);
/*after that, use set to replace old value with the new one*/
int index = 4;
mData.set(index, detail);
/*finally, refresh your adapter*/
if(adapter!=null)
adapter.notifyItemChanged(index);
You can use the notifyItemChanged(int position) method from the RecyclerView.Adapter class. From the documentation:
Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);.
This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.
As you already have the position, it should work for you.
Below solution worked for me:
On a RecyclerView item, user will click a button but another view like TextView will update without directly notifying adapter:
I found a good solution for this without using notifyDataSetChanged() method, this method reloads all data of recyclerView so if you have image or video inside item then they will reload and user experience will not good:
Here is an example of click on a ImageView like icon and only update a single TextView (Possible to update more view in same way of same item) to show like count update after adding +1:
// View holder class inside adapter class
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageViewLike;
public MyViewHolder(View itemView) {
super(itemView);
imageViewLike = itemView.findViewById(R.id.imageViewLike);
imageViewLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition(); // Get clicked item position
TextView tv = v.getRootView().findViewById(R.id.textViewLikeCount); // Find textView of recyclerView item
resultList.get(pos).setLike(resultList.get(pos).getLike() + 1); // Need to change data list to show updated data again after scrolling
tv.setText(String.valueOf(resultList.get(pos).getLike())); // Set data to TextView from updated list
}
});
}