Skip to content

Instantly share code, notes, and snippets.

@AL4AL
Created November 9, 2019 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AL4AL/1481bfd41b00e11aa8e8b2e091279fe1 to your computer and use it in GitHub Desktop.
Save AL4AL/1481bfd41b00e11aa8e8b2e091279fe1 to your computer and use it in GitHub Desktop.
RecyclerView with two ViewHolders
public class VenueListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VENUE_ITEM_VIEW_TYPE = 1;
private static final int EMPTY_LIST_VIEW_TYPE = 2;
private List<Venue> mListData = new ArrayList<>();
public VenueListAdapter(List<Venue> listData) {
if (listData != null){
this.mListData.addAll(listData);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case VENUE_ITEM_VIEW_TYPE: {
RowVenueBinding rowVenueBinding = RowVenueBinding.inflate(inflater);
rowVenueBinding.getRoot().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return new VenueViewHolder(rowVenueBinding);
}
default: { // EMPTY_LIST_VIEW_TYPE ( I used default so there is no need to additional return
ViewEmptyStateBinding viewEmptyStateBinding = ViewEmptyStateBinding.inflate(inflater);
viewEmptyStateBinding.getRoot().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return new EmptyListViewHolder(viewEmptyStateBinding);
}
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == EMPTY_LIST_VIEW_TYPE) return;
((VenueViewHolder)holder).bind(getItem(position));
Bundle venueItemBundle = new Bundle();
venueItemBundle.putString(
holder.itemView.getContext().getString(R.string.key_venue_id),
getItem(position).id);
}
@Override
public int getItemCount() {
if (mListData.size() == 0)
return 1;
return mListData.size();
}
@Override
public int getItemViewType(int position) {
if (mListData.size() == 0) {
return EMPTY_LIST_VIEW_TYPE;
} else {
return VENUE_ITEM_VIEW_TYPE;
}
}
private Venue getItem(int position) {
return mListData.get(position);
}
public void submitList(@NonNull List<Venue> list) {
this.mListData = list;
DiffUtil.calculateDiff(new VenueListDiffCallback(this.mListData, list)).dispatchUpdatesTo(this);
}
public List<Venue> getListData() {
return this.mListData;
}
class VenueViewHolder extends RecyclerView.ViewHolder {
private RowVenueBinding venueBinding;
VenueViewHolder(RowVenueBinding venueBinding) {
super(venueBinding.getRoot());
this.venueBinding = venueBinding;
}
void bind(Venue venue) {
venueBinding.setVenue(venue);
}
}
class EmptyListViewHolder extends RecyclerView.ViewHolder {
EmptyListViewHolder(ViewEmptyStateBinding emptyStateBinding) {
super(emptyStateBinding.getRoot());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment