Daniel
Daniel

Reputation: 1033

RecyclerView Adapter's data

So I have a RecyclerView and its adapter. Any changes of the RecyclerView are held in activity and adapter only notifies the activity which(and how) item of the RecyclerView has to be changed.

Here's an example of what's happening in the activity:

list.remove(position);
adapter.notifyItemRemoved(position);
adapter.notifyItemRangeChanged(position, list.size());

So the list data of the activity is being updated by calling list.remove(position) And there is a question. Should I also update the adapter's list data and how should I do that?

Upvotes: 0

Views: 194

Answers (1)

Vipul Asri
Vipul Asri

Reputation: 9213

Firstly should not modify the items of ArrayList in your Activity, as this will make changes to your Activity's ArrayList not your RecyclerView Adapter. Pass the ArrayList to RecyclerView Adapter in its constructor. (e.g. feedItemList).

Then you can have a method onItemDismiss() in RecyclerView Adapter with following method definition and call it where you want to remove the row.

public void onItemDismiss(int position) {
    if(position!=-1 && position<feedItemList.size())
    {
        feedItemList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount());
    }
}

@Override
public int getItemCount() {
    return (null != feedItemList ? feedItemList.size() : 0);
}

Upvotes: 1

Related Questions