Taran Vohra
Taran Vohra

Reputation: 217

How does RecyclerView handle recycling with different views?

How does it handle recycling when we have heterogeneous views in a RecyclerView? For example in the following code, we are creating 3 different types of views to be used in a recycler view. Now after these views have been inflated, how does recyclerView know not to use these views for position 1 & 2 for rest of the items?

private class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int TYPE_1 = 1;
private static final int TYPE_2 = 2;
private static final int TYPE_3 = 3;
// more types...

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case TYPE_1: inflate view_type_1
            break;
        case TYPE_2: inflate view_type_2
            break;
        case TYPE_3: inflate view_type_3
            break;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)) {
        case TYPE_1:
            break;
        case TYPE_2:
            break;
        case TYPE_3:
            break;
    }
}

@Override
public int getItemViewType(int position) {
    if(position == 1) {
        return TYPE_1;
    } else if(position == 2) {
        return TYPE_2;
    } else 
        return TYPE_3
}
}

Upvotes: 2

Views: 2025

Answers (3)

sahil goyal
sahil goyal

Reputation: 26

Recycler view has a method On create view in that method you are getting the same view type that you are returning from the ( something like get viewitem type) and in on create view just implement the same checks as like get item type method and In on create view just pass the view to the objects of view holder as according to the
View type. Just try to take diff. View holder for different view

Upvotes: 0

Samuele Pontremoli
Samuele Pontremoli

Reputation: 594

I don't get your question fully. Here's an explaination of how RecyclerView works:

RecyclerView.Adapter keeps a pool of views. While possible, it will use one of the empty instances it has. When it's not possible (the pool is empty) it will create a new one. But how does this pool get empty?

When a view is attached to the ReciclerView, it will be removed from the pool to be added back again only when you're not displaying it -> Reason why you should reset everything in the view.

So basically RecyclerView doesn't know anything about your views. It will either use a new View, or a Recycled one taken from the pool in the adapter when possible. Then inflate what you told the view to do in the onBindViewHolder. If the view has some properties in it, like color or text or anything else, if you do not reset it, it will remain in the recycled view.

EDIT

As Gabe Sechan said, the correct view type is determined by the method getItemViewType(int position) that returns the correct viewtype to be inflated.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93708

The RecyclerView basically keeps multiple pools of View objects, one pool for every value returned by getItemViewType. When it needs a view for a new item, it first calls getItemViewType. It then goes to the appropriate pool for that result (if none exists it makes a new one) and grabs a view from it. It remembers what type each view on screen was. When a view is cycled off the screen, its returned to the correct pool.

Upvotes: 6

Related Questions