Skip to content

Instantly share code, notes, and snippets.

@RockerFlower
Created December 11, 2014 08:17
Show Gist options
  • Save RockerFlower/7ba1672424aec00ef079 to your computer and use it in GitHub Desktop.
Save RockerFlower/7ba1672424aec00ef079 to your computer and use it in GitHub Desktop.
A RecyclerView with multiple view type.
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 );
}
}
public class ImageViewHolder extends MainViewHolder {
@InjectView ( R.id.groupImage )
ImageView mImage;
public ImageViewHolder ( View itemView ) {
super ( itemView );
ButterKnife.inject ( this, itemView );
}
}
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 );
}
}
public class MainViewHolder extends RecyclerView.ViewHolder {
public ViewHolder ( View itemView ) {
super ( itemView );
}
}
@kaveeshkanwal
Copy link

What is ViewHolderGuide?

@kaveeshkanwal
Copy link

Update:
I had just put my custom holder class instead of ViewHolderGuide and it worked.
Thanks a lot!

@vivchar
Copy link

vivchar commented Jan 28, 2018

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

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