Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...