How to add fixed Button in RecyclerView Adapter?

一个人想着一个人 提交于 2019-12-19 08:53:38

问题


nav_draw_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

  <TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="30dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textSize="15dp"
    android:textStyle="bold"
    android:focusable="true"
    android:outlineProvider="bounds" />
</RelativeLayout>

Now I want to add a fixed Button in the end of nav_draw_row but whenever I am adding button in above code just after TextView. It is showing in following way:

I am aware why this is happening but not able to find out the solution to make Button fix in the end of Bar. Here is my Adapter code:

public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {

List<NavDrawerItem> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;

    public NavigationDrawerAdapter(Context context, List<NavDrawerItem> data) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    public void delete(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        NavDrawerItem current = data.get(position);
        holder.title.setText(current.getTitle());
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }
   }

How to fix this problem?


回答1:


Build a Load More Button into a recyclerview.

The first thing to do is to create a layout with your button and modify your viewhold to get the button id:

Then a simple way to make a button is to add +1 to the getItemCount() and a flag (i.e. boolean) to know if you need to draw the button.

The new getItemCount with the flag will look like this:

private boolean hasLoadButton = true;

public boolean isHasLoadButton() {
    return hasLoadButton;
}

public void setHasLoadButton(boolean hasLoadButton) {
    this.hasLoadButton = hasLoadButton;
    notifyDataSetChanged();
}

@Override
public int getItemCount() {
    if (hasLoadButton) {
        return data.size() + 1;
    } else {
        return data.size();
    }
}

Then you have to override into the adapter the method to get the type of the view:

private final int TITLE = 0;
private final int LOAD_MORE = 1;
@Override                                 
public int getItemViewType(int position) {
    if (position < getItemCount()) {     
        return TITLE;                         
    } else {                              
        return LOAD_MORE;                         
    }                                     
}  

If the position is between 0 and data.size()-1, you will have a typeView for the title, else a typeview for the load more button.

Now we have to create the view holder:

In the method onCreateViewHolder(ViewGroup parent, int viewType), y

@Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TITLE) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.nav_draw_row, parent, false));
        } else if (view type == LOAD_MORE) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more_row, parent, false));
        } else {
            return null;
        }
    }

load_more_row

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:id="@+id/load_more"
        android:text="load more !"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Don't forget to change your viewholder to include the new button.

And finally the method onBindViewHolder :

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(position >= getItemCount) {
        holder.loadMore....
    } else {
    NavDrawerItem current = data.get(position);
    holder.title.setText(current.getTitle());
    }
}

Hope you can do what you want with the recyclerview !



来源:https://stackoverflow.com/questions/32467638/how-to-add-fixed-button-in-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!