Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Created August 16, 2014 14:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielemariotti/e9114dec8e82480c874e to your computer and use it in GitHub Desktop.
Save gabrielemariotti/e9114dec8e82480c874e to your computer and use it in GitHub Desktop.
RecyclerView and ViewHolder based on viewType
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerView.MyViewHolder>{
public static class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType){
case 0:
view = LayoutInflater.from(mContext).inflate(mRowLayout0, parent, false);
break;
case 1:
view = LayoutInflater.from(mContext).inflate(mRowLayout1, parent, false);
break;
//......
}
return new MyViewHolder(view);
}
@Override
public int getItemViewType(int position) {
//Implement your logic here
MyObject obj = (MyObject) getItem(position);
return obj.getType();
}
}
@captrespect
Copy link

It might be better to put the switch inside getItemViewType and return the layout that needs inflated.
Then you can simply inflate the view passed into onCreateViewHolder and have a the switch use a well defined enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment