5

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: enter image description here

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?

Community
  • 1
  • 1

2 Answers2

2

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),

@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());
    }
}
Triaro
  • 90
  • 1
  • 7
xiaomi
  • 6,553
  • 3
  • 29
  • 34
  • Thanks for the answer but I do have a `TextView` in my layout which I cannot see here –  Sep 09 '15 at 13:31
  • Is the text view located above the recycler view ? – xiaomi Sep 09 '15 at 13:38
  • I have pasted my layout file and code as well. You can look at the `onBindViewHolder ` method in Adapter code –  Sep 09 '15 at 13:54
  • I don't really understand your layout (what is inside a row, what is outside the recycler view), when you say "make Button fix in the end of Bar", what do you mean ? – xiaomi Sep 09 '15 at 14:07
  • If I run the above layout then it will show a list of String i.e. `My listing` `Member Benefits` `your Listing` .... What I want to add Button in the end of List. –  Sep 09 '15 at 14:17
  • One example could be ..Recycler-view showing 10 list and there should be a `Load more` button to load next 10 list –  Sep 09 '15 at 14:21
  • I'm trying to do the same thing. Where am I supposed to use setHasLoadButton/isHasLoadButton. I did all the steps, but I have no idea where to put those. Could you edit with a more detailed answer ? Thank you. – Bogdan Daniel Jan 30 '16 at 22:57
0

You can create a custom view that extends the recycler view class create linear layout add a button and a recycler view create class for the new view use your new custom view in XML and add the listener to the button and that's it hope that helps to give you an idea

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140