Last active
December 14, 2015 15:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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