How to add a fixed button to a RecyclerView adapter?

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 one Buttonat the end nav_draw_row, but whenever I add a button to the code above right after TextView. It shows as follows: enter image description here

I know why this is happening, but I could not find a solution to make a correction Buttonat the end Bar. Here is my code Adapter:

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 solve this problem?

+4
source share
1 answer

Create an additional download button in recyclerview.

, , , :

- +1 getItemCount() ( ), , .

getItemCount :

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();
    }
}

:

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;                         
    }                                     
}  

0 data.size() - 1, typeView , .

:

onCreateViewHolder ( ViewGroup, 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>

, .

, , 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());
    }
}

, , , recyclerview!

+2

Source: https://habr.com/ru/post/1606503/


All Articles