Reputation: 71
With two view types, my recycler view can display a header content and the main content. I am lost on what the best way to organize these two in the adapter and thus have control on which view comes first. My current problem is having the header before the main content. Every time my header displays at the bottom which renders it useless.
//Declaration
private static final int HEADER = 0;
private static final int TOP_PICKS = 1;
//getViewtype
@Override
public int getItemViewType(int position) {
if (position < mMainContentList.size()) {
return MAIN_CONTENT;
}
return HEADER;
}
//getItemCount
@Override
public int getItemCount() {
if (mHeaderItems == null) {
return mMainContentList.size();
} else {
return mMainContentList.size() + 1;
}
}
What am I missing?
Upvotes: 0
Views: 38
Reputation: 4371
Try like this
@Override
public int getItemViewType(int position) {
if (position == 0) {
return HEADER;
}
return MAIN_CONTENT;
}
@Override
public int getItemCount() {
if (mHeaderItems == null) {
return mMainContentList.size();
} else {
return mMainContentList.size() + 1;
}
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup pParent, int viewType) {
LayoutInflater lLayoutInflater = LayoutInflater.from(pParent.getContext());
switch (viewType) {
case HEADER:
// inflate header view
break;
case MAIN_CONTENT:
// inflate main content view
break;
default:
// inflate main content view
}
}
Upvotes: 1