Reputation: 13
Basically, I need to add a button after the last element in the recyclerview. That's because the user can push this button to load more content from the server.
I using cards por show the information in the recycler view, and I trying to inflate a special card, with the button that I need, but I don't know how inflate this card after to inflate the last card using the cursor data.
Upvotes: 1
Views: 6793
Reputation: 256
You will have to override getItemViewType(int position)
in your adapter.
public final int CONTENT_TYPE = 1;
public final int BUTTON_TYPE = 2;
Lets say your cursor count is 'x'. For positions 0 to (x-1), you will inflate the cardview with the cursor content. Hence, getItemViewType
will return CONTENT_TYPE for positions 0 to (x-1). Next, for position 'x', return BUTTON_TYPE.
You will get hold of the view type in the onCreateXXX
method of your adapter. Based on the view type, inflate the right xml (button or cursor content).
Be sure to return cursor count + 1 in the getItemCount() method. (+1 being for the button layout).
Upvotes: 2
Reputation: 11
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
<Button
android:id="@+id/load_more"
android:text="load more !"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
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());
}
}
Upvotes: 1