Skip to content

Instantly share code, notes, and snippets.

@ChanSek
Last active May 16, 2020 21:53
Show Gist options
  • Save ChanSek/626d33ec4aaad7aad667db90dadc8d00 to your computer and use it in GitHub Desktop.
Save ChanSek/626d33ec4aaad7aad667db90dadc8d00 to your computer and use it in GitHub Desktop.
FirebaseRecyclerAdapter with multiple view type supported
mAdapter = new FirebaseRecyclerAdapter<User, RecyclerView.ViewHolder>(
User.class, R.layout.item_user, RecyclerView.ViewHolder.class, ref) {
@Override
protected void populateViewHolder(final RecyclerView.ViewHolder viewHolder, final User user,
final int position) {
switch (user.getType()) {
case Constants.USER_TYPE_1:
populateType1((ViewHolder1) viewHolder, user, position);
break;
case Constants.USER_TYPE_2:
populateType2((ViewHolder2) viewHolder, user, position);
break;
case Constants.USER_TYPE_3:
populateType3((ViewHolder3) viewHolder, user, position);
break;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case USER_TYPE_1:
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_type1, parent, false);
return new ViewHolder1(userType1);
case USER_TYPE_2:
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_type2, parent, false);
return new ViewHolder2(userType2);
case USER_TYPE_3:
View userType3 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_type3, parent, false);
return new ViewHolder3(userType3);
}
return super.onCreateViewHolder(parent, viewType);
}
@Override
public int getItemViewType(int position) {
User user = getItem(position);
switch (user.getType()) {
case Constants.USER_TYPE_1:
return USER_TYPE_1;
case Constants.USER_TYPE_2:
return USER_TYPE_2;
case Constants.USER_TYPE_3:
return USER_TYPE_3;
}
return super.getItemViewType(position);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment