Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can recyclerView have the last row with custom item that does not same with the other items?

I have a layout with order list and last row is the total price of those order. And I don't have an idea that how can I do those list items and last row with my desire custom item, together in a recycler view. Do I make some logic in onBindViewHolder? Or, does it have another way, one of the RecyclerView methods?

like image 561
KyawLinnThant Avatar asked Jan 29 '18 08:01

KyawLinnThant


2 Answers

You can do this by using below code on your RecyclerAdapter class

@Override
public int getItemViewType(int position) {
    if(position==(getItemCount()-1))return 1;
    else return 2;

}

In onCreateViewHolder inflate your last layout according to your viewType.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == 1) {
        // inflate your first item layout & return that viewHolder
    } else {
        // inflate your second item layout & return that viewHolder
    }
}
like image 66
Abhishek kumar Avatar answered Sep 22 '22 17:09

Abhishek kumar


Yes, you can do that using ItemTypes,RecyclerView can render different type of child views, Please refer to this example : RecyclerView With Multiple Item Types

like image 32
Rajan Kali Avatar answered Sep 19 '22 17:09

Rajan Kali