简体   繁体   中英

Refreshing a RecyclerView when all items are removed

I have a RecyclerView that shows a list of items.

If there is no item to show, The recyclerview shows one item with a specific view (to tell the user there is no item instead of a white screen).

Within HistoryFragment :

private void initRecyclerView(Boolean isNoResult){
        HistoryRecyclerViewAdapter adapter = new HistoryRecyclerViewAdapter(mContext, mRecords, **isNoResult**);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(adapter);
}

Within HistoryRecyclerViewAdapter:

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;

        if(**isEmpty**) {
            **view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory_empty, parent, false);**
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory, parent, false);
        }

        ViewHolder holder = new ViewHolder(view);
        return holder;
}

So, it's possible to remove items one by one, if we click on them.

I would like to set isEmpty to true and refresh the RecyclerView when the dataSet is null.

I already know where to call that method but I really don't know how I can do that? (ie refresh the RecyclerView with isEmpty = true so I can display the cell that explain to the user that there is no record anymore).

Don't inflate different view-holders, because when the adapter has no items, not a single one of them will ever be inflated. Instead one can wrap the RecyclerView together with a "no data" Fragment into a ViewFlipper , which then can be switched to the Fragment , when the RecyclerView adapter has no items.

Best practice is to use an empty view outside of RecyclerView but in case you like to do what you want:

1.in onCreateViewHolder only inflate one layout which has empty and item views

  1. on item delete check if your array is empty and then add a null item then in onBindViewHolder check the model if model is Null visible empty view otherwise show item view

summary:

onBind:

model is null: empty View visible

model is not null: item View visible

use interface to refresh the RecyclerView after remove something like this

public interface RefreshRecyclerView {

    public void Refresh();
}

then in activity or fragment implement the interface

Fragment implements RefreshRecyclerView

you will have override method like this

 @Override
    public void Refresh() {
       // set adapter again here 
    }

then pass the interface to adapter like this

RefreshRecyclerView refresh = (RefreshRecyclerView) this;

yourRecycler.setadapter(refresh);

fially when user clicked on adapter use this

refresh. Refresh();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM