Skip to content Skip to sidebar Skip to footer

Organize Views In RecyclerView By Their Position

So this is my FeedAdapter: public class FeedAdapter extends RecyclerView.Adapter { private final int VIEW_TYPE_VERTICAL = 1; private final

Solution 1:

Sort/construct your items list so that it has the items in the correct order.

Ideally, you'd start with your three types partitioned (i.e. one list of Vertical items, a second list of Horizontal items, and a third list of Ad items). Then you could build your overall list like this:

this.items = new ArrayList<>();

if (listVertical.size() > 0) {
    items.add(listVertical.remove(0));
}

if (listHorizontal.size() > 0) {
    items.add(listHorizontal.remove(0));
}

while (listVertical.size() > 0 || listAd.size() > 0) {    
    if (listAd.size() > 0) {
        items.add(listAd.remove(0));
    }

    int count = 5;

    while (count > 0 && listVertical.size() > 0) {
        items.add(listVertical.remove(0));
    }
}

This will add one vertical view, then one horizontal view, and then one ad view + five vertical views in a loop until you "run out" of both ad views and vertical views. I believe that matches your desired structure.

At the end, your three lists will be empty (because you will have remove()d all items from them). If you need to avoid this, you can create copies:

List<Vertical> verticalCopy = new ArrayList<>(listVertical);
// and so on

And then you can just use verticalCopy instead of listVertical in the above code.


Post a Comment for "Organize Views In RecyclerView By Their Position"