Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button at the end of RecyclerView?

I want to show a button at the end of RecyclerView.

With ListView there was a method addFooterView(), how to do the same with RecylerView.

like image 643
Vrutin Rathod Avatar asked Mar 17 '15 18:03

Vrutin Rathod


People also ask

What are recyclerviews in Android?

RecyclerViews were introduced with Android L (2014) as a support library. They are much more flexible and allows you to do more than ListView. It allows you to use custom Layout Manager and also has default implementations like LinearLayoutManager (to get what ListView lets you do).

How many components are there in recyclerview?

It has removed all the major tasks from it and gave it to all the other members so that RecyclerView Class itself remains light unlike ListView. Thus, there are major 3 components of Recycler View and I’d like to add one in the list, so in total of 4 Components.

What makes Google recyclerview better than listview?

What Google has tried to achieve with a RecyclerView is (drum roll please!) modularity. It has removed all the major tasks from it and gave it to all the other members so that RecyclerView Class itself remains light unlike ListView.

How do I add a button to a cardview?

To add button to you first need to add a cardview or any view group Relative Layout, Linear Layout or any viewgroup. Add button to it then inflate the layout. I cant explain you in detail, you need to study further more in detail. Should I hire remote software developers from Turing.com?


1 Answers

I came across this problem and found this answer. The current selected answer is correct but it is missing some details. Here is the full implementation,

Add this method to your adapter

@Override public int getItemViewType(int position) {     return (position == myItems.size()) ? R.layout.button : R.layout.item; } 

It will check if the current position is past the last item in your list, if it is then it will return the button layout value to this method,

@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      View itemView;      if(viewType == R.layout.item){         itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);     }      else {         itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.button, parent, false);     }      return new MyViewHolder(itemView); } 

The above method checks if the passed viewType is the item layout or the button layout and then inflates the view occordingly and sends the inflated view to this method,

@Override public void onBindViewHolder(MyViewHolder holder, int position) {     if(position == myItems.size()) {         holder.button.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();             }         });     }     else {         final String name = myItems.get(position);         holder.title.setText(name);     } } 

This checks if we are past the last item in the list and if we are then it sets the onClick method to our button. Otherwise it creates an item like normal.

We also need to change the getItemCount method to this

@Override public int getItemCount() {     return myItems.size() + 1; } 

Since we are going through all the items and then adding a button at the end, the + 1 is our button.

Then lastly we need to add our view to the myViewHolder class

public class MyViewHolder extends RecyclerView.ViewHolder {     public TextView title;     public Button button;      public MyViewHolder(View view) {         super(view);         title  = (TextView) view.findViewById(R.id.title);         button = (Button) view.findViewById(R.id.bottom_button);     } } 

I hope this helps!

like image 136
Gary Holiday Avatar answered Sep 18 '22 12:09

Gary Holiday