Created
August 16, 2014 14:38
-
-
Save gabrielemariotti/e9114dec8e82480c874e to your computer and use it in GitHub Desktop.
RecyclerView and ViewHolder based on viewType
This file contains hidden or 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 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.