Skip to content

Instantly share code, notes, and snippets.

@davidjrichardson
Last active December 14, 2015 15:08
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 davidjrichardson/5c9990e5f0f3858861c4 to your computer and use it in GitHub Desktop.
Save davidjrichardson/5c9990e5f0f3858861c4 to your computer and use it in GitHub Desktop.
RecyclerView adapter that would allow for the view to have varying view item layouts for all items.
public final HomogenousViewAdapter extends RecyclerView.adapter<ViewHolder> {
// This could be enhanced with an IntDef for static analysis, though its not strictly necessary
private int viewSelector = 0;
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View rootView;
// viewType is determined by getItemViewType() which has been overridden below
switch(viewType) {
case 0:
rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_layout_1, parent, false);
case 1:
... // Same as above with different view layout resource
default:
... // Default view layout resource
}
return new ViewHolder(rootView);
}
// Other RecyclerView functions goes here
@Override
public int getItemViewType(final int position) {
return viewSelector;
}
public void setViewSelector(final int type) {
viewSelector = type;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment