I need to realize a behavior where I have a RecyclerView with pictures and the last picture has to be permanent, like a button of adding the new picture to the stack.
Asked
Active
Viewed 26 times
0
-
1See [this](https://stackoverflow.com/a/26245463/6287910) Stack Overflow answer. You would return the count of your items plus one and use a different view holder for the last item which is your "+". [This post](https://stackoverflow.com/a/38691600/6287910) may also be useful. – Cheticamp Jun 28 '23 at 00:54
2 Answers
0
in the adapter, you can combine onCreateViewHolder, with GetItemViewType like so:
@Override
public int getItemViewType(int position) {
if(position == getItemCount())
return TYPE_FOOTER;
else if(position == 0)
return TYPE_HEADER;
else
return TYPE_BODY
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch(viewType){
case TYPE_FOOTER:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_grocery_list_item, parent, false);
return new FooterViewHolder(view);
case TYPE_HEADER:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_grocery_list_item, parent, false);
return new HeaderViewHolder(view);
case TYPE_BODY:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_grocery_list_item, parent, false);
return new BodyViewHolder(view);
}
}
you'll need to define TYPE_FOOTER, TYPE_HEADER, and TYPE_BODY as constants that suit you, I don't think it matters. as long as you remain consistent. make sure you manage the getItemCount() function correctly. if the items are size X and you want a footer, don't forget to return x+1 in the getItemCount() function.
good luck with your app!

EZH
- 63
- 6
0
You'll need to handle view types and manage the count for adding custom footer as suggested in the answer by EZH
Although, there's an easier way to handle such use cases by using Groupie https://github.com/lisawray/groupie

Anshul Khattar
- 71
- 2