-4

I want to have a button at the end of my recyclerview at all times. Basically the user can add more items in the recyclerview and delete them as well but the button should always be at the end. I'm pretty sure I dont add it to my row.xml because it will just make each items in the list have a button.

KristoferH
  • 19
  • 7

1 Answers1

0

Easiest way in my opinion is to have the button and a content container in your recyclerview item layout, default with the button's visibility set to gone and the content's layout visible. When displaying the last item, just set the layout's visibility gone and the button's visible.

Or you can use 2 different layout files altogether, like in this sample adapter:

public class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int VIEW_TYPE_BUTTON = 0;
    public static final int VIEW_TYPE_ITEM = 1;

    private List<Object> items = Collections.emptyList();

    public void setData(List<YourItem> itemList) {
        items.clear();
        items.addAll(itemList);
        items.add("button");
        notifyDataSetChanged();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView tvItemDescription; // just an example

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvItemDescription = itemView.findViewById(R.id.tv_description);
        }
    }

    public class ButtonViewHolder extends RecyclerView.ViewHolder {
        public Button button;

        public ButtonViewHolder(View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
            button.addOnClickListener(/*add logic what should happen when the button is clicked*/);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        switch (viewType) {
            case VIEW_TYPE_ITEM:
                return new ItemViewHolder(inflater.inflate(R.layout.item, parent, false));

            case VIEW_TYPE_BUTTON:
                return new ButtonViewHolder(inflater.inflate(R.layout.item_button, parent, false));

            default:
                throw new IllegalArgumentException("no such view type defined");
        }

        return null;
    }

    @Override
    public int getItemViewType(int position) {
        Object item = items.get(position);

        return (item instanceof YourItem) ? VIEW_TYPE_ITEM : VIEW_TYPE_BUTTON;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object item = items.get(position);

        if (item instanceof YourItem) {
            YourItem yourItem = (YourItem) item;
            YourAdapter.ItemViewHolder itemViewHolder = (ItemViewHolder) holder;

            itemViewHolder.tvItemDescription.setText(yourItem.description);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}

In this case, by adding an item at the end of the adapter's data set which ist not of type YourItem, the recyclerview will show an item inflated as defined in item_button.xml. Haven't actually tried the code myself, but see if you can get this running.

kazume
  • 1,333
  • 2
  • 16
  • 30
  • How do I use two layouts into one adapter and having the layout with the button to always be at the last. Sorry Im kind of new to android and coding in general – KristoferH Feb 26 '18 at 03:57