skip to Main Content

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.

enter image description here

2

Answers


  1. 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!

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search