Created
December 11, 2014 08:17
-
-
Save RockerFlower/7ba1672424aec00ef079 to your computer and use it in GitHub Desktop.
A RecyclerView with multiple view type.
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 class GroupViewHolder extends MainViewHolder { | |
@InjectView ( R.id.groupTitle ) | |
TextView mTitle; | |
@InjectView ( R.id.groupContent ) | |
TextView mContent; | |
public GroupViewHolder ( View itemView ) { | |
super ( itemView ); | |
ButterKnife.inject ( this, itemView ); | |
} | |
} |
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 class ImageViewHolder extends MainViewHolder { | |
@InjectView ( R.id.groupImage ) | |
ImageView mImage; | |
public ImageViewHolder ( View itemView ) { | |
super ( itemView ); | |
ButterKnife.inject ( this, itemView ); | |
} | |
} |
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 class MainAdapter extends RecyclerView.Adapter< MainAdapter.ViewHolder > { | |
private ArrayList< Group > groups = new ArrayList<> (); | |
private static final int TYPE_IMAGE = 0; | |
private static final int TYPE_GROUP = 1; | |
@Override | |
public int getItemViewType ( int position ) { | |
int viewType; | |
if ( groups.get ( position ).getImagePath () != null ) { | |
viewType = TYPE_IMAGE; | |
} else { | |
viewType = TYPE_GROUP; | |
} | |
return viewType; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder ( ViewGroup viewGroup, int viewType ) { | |
LayoutInflater mInflater = LayoutInflater.from ( viewGroup.getContext () ); | |
switch ( viewType ) { | |
case TYPE_IMAGE: | |
ViewGroup vImage = ( ViewGroup ) mInflater.inflate ( R.layout.item_image, viewGroup, false ); | |
ImageHolder vhImage = new ImageViewHolder ( vImage ); | |
return vhImage; | |
case TYPE_GROUP: | |
ViewGroup vGroup = ( ViewGroup ) mInflater.inflate ( R.layout.item_group, viewGroup, false ); | |
GroupViewHolder vhGroup = new GroupViewHolder ( vGroup ); | |
return vhGroup; | |
default: | |
ViewGroup vGroup0 = ( ViewGroup ) mInflater.inflate ( R.layout.item_group, viewGroup, false ); | |
GroupViewHolder vhGroup0 = new GroupViewHolder ( vGroup0 ); | |
return vhGroup0; | |
} | |
} | |
@Override | |
public void onBindViewHolder ( ViewHolder viewHolder, int i ) { | |
switch ( viewHolder.getItemViewType () ) { | |
case TYPE_IMAGE: | |
ImageViewHolder imageViewHolder = ( ViewHolderGuide ) viewHolder; | |
imageViewHolder.mImage.setImageResource ( R.drawable.img_placeholder ); | |
break; | |
case TYPE_GROUP: | |
GroupViewHolder groupViewHolder = ( GroupViewHolder ) viewHolder; | |
groupViewHolder.mContent.setText ( groups.get ( i ).getContent() ); | |
groupViewHolder.mTitle.setText ( groups.get ( i ).getTitle () ); | |
break; | |
} | |
} | |
@Override | |
public int getItemCount () { | |
return groups.size (); | |
} | |
public MainAdapter ( List< Group > groups ) { | |
this.groups = new ArrayList<> ( groups ); | |
} | |
} |
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 class MainViewHolder extends RecyclerView.ViewHolder { | |
public ViewHolder ( View itemView ) { | |
super ( itemView ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is good that you created a multiple view type adapter, but the best way it is to use any library, for example https://github.com/vivchar/RendererRecyclerViewAdapter