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.
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.
2
Answers
in the adapter, you can combine
onCreateViewHolder, with GetItemViewType like so:
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!
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