Skip to main content
Active reading. Dressed the naked link.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Yes, it is possible. In

In your adapter getItemViewType Layout like this ....

 public class MultiViewTypeAdapter extends RecyclerView.Adapter {     private ArrayList<Model>dataSet;   Context mContext;   int total_types;   MediaPlayer mPlayer;   private boolean fabStateVolume = false;     public static class TextTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   CardView cardView;     public TextTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.cardView = (CardView) itemView.findViewById(R.id.card_view);   }   }     public static class ImageTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   ImageView image;     public ImageTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.image = (ImageView) itemView.findViewById(R.id.background);   }   }     public static class AudioTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   FloatingActionButton fab;     public AudioTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.fab = (FloatingActionButton) itemView.findViewById(R.id.fab);   }   }     public MultiViewTypeAdapter(ArrayList<Model>data, Context context) {   this.dataSet = data;   this.mContext = context;   total_types = dataSet.size();   }     @Override   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     View view;   switch (viewType) {   case Model.TEXT_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false);   return new TextTypeViewHolder(view);   case Model.IMAGE_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false);   return new ImageTypeViewHolder(view);   case Model.AUDIO_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_type, parent, false);   return new AudioTypeViewHolder(view);   }   return null;   }     @Override   public int getItemViewType(int position) {     switch (dataSet.get(position).type) {   case 0:   return Model.TEXT_TYPE;   case 1:   return Model.IMAGE_TYPE;   case 2:   return Model.AUDIO_TYPE;   default:   return -1;   }   }     @Override   public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {     Model object = dataSet.get(listPosition);   if (object != null) {   switch (object.type) {   case Model.TEXT_TYPE:   ((TextTypeViewHolder) holder).txtType.setText(object.text);     break;   case Model.IMAGE_TYPE:   ((ImageTypeViewHolder) holder).txtType.setText(object.text);   ((ImageTypeViewHolder) holder).image.setImageResource(object.data);   break;   case Model.AUDIO_TYPE:     ((AudioTypeViewHolder) holder).txtType.setText(object.text);     }   }   }     @Override   public int getItemCount() {   return dataSet.size();   }  } 

forFor the reference link  : https://www.journaldev.com/12372/android-recyclerview-exampleAndroid RecyclerView Example – Multiple ViewTypes

Yes, it is possible. In your adapter getItemViewType Layout like this ....

 public class MultiViewTypeAdapter extends RecyclerView.Adapter {     private ArrayList<Model>dataSet;   Context mContext;   int total_types;   MediaPlayer mPlayer;   private boolean fabStateVolume = false;     public static class TextTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   CardView cardView;     public TextTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.cardView = (CardView) itemView.findViewById(R.id.card_view);   }   }     public static class ImageTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   ImageView image;     public ImageTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.image = (ImageView) itemView.findViewById(R.id.background);   }   }     public static class AudioTypeViewHolder extends RecyclerView.ViewHolder {     TextView txtType;   FloatingActionButton fab;     public AudioTypeViewHolder(View itemView) {   super(itemView);     this.txtType = (TextView) itemView.findViewById(R.id.type);   this.fab = (FloatingActionButton) itemView.findViewById(R.id.fab);   }   }     public MultiViewTypeAdapter(ArrayList<Model>data, Context context) {   this.dataSet = data;   this.mContext = context;   total_types = dataSet.size();   }     @Override   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     View view;   switch (viewType) {   case Model.TEXT_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false);   return new TextTypeViewHolder(view);   case Model.IMAGE_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false);   return new ImageTypeViewHolder(view);   case Model.AUDIO_TYPE:   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_type, parent, false);   return new AudioTypeViewHolder(view);   }   return null;   }     @Override   public int getItemViewType(int position) {     switch (dataSet.get(position).type) {   case 0:   return Model.TEXT_TYPE;   case 1:   return Model.IMAGE_TYPE;   case 2:   return Model.AUDIO_TYPE;   default:   return -1;   }   }     @Override   public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {     Model object = dataSet.get(listPosition);   if (object != null) {   switch (object.type) {   case Model.TEXT_TYPE:   ((TextTypeViewHolder) holder).txtType.setText(object.text);     break;   case Model.IMAGE_TYPE:   ((ImageTypeViewHolder) holder).txtType.setText(object.text);   ((ImageTypeViewHolder) holder).image.setImageResource(object.data);   break;   case Model.AUDIO_TYPE:     ((AudioTypeViewHolder) holder).txtType.setText(object.text);     }   }   }     @Override   public int getItemCount() {   return dataSet.size();   }  } 

for reference link  : https://www.journaldev.com/12372/android-recyclerview-example

Yes, it is possible.

In your adapter getItemViewType Layout like this ....

public class MultiViewTypeAdapter extends RecyclerView.Adapter { private ArrayList<Model>dataSet; Context mContext; int total_types; MediaPlayer mPlayer; private boolean fabStateVolume = false; public static class TextTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; CardView cardView; public TextTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.cardView = (CardView) itemView.findViewById(R.id.card_view); } } public static class ImageTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; ImageView image; public ImageTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.image = (ImageView) itemView.findViewById(R.id.background); } } public static class AudioTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; FloatingActionButton fab; public AudioTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.fab = (FloatingActionButton) itemView.findViewById(R.id.fab); } } public MultiViewTypeAdapter(ArrayList<Model>data, Context context) { this.dataSet = data; this.mContext = context; total_types = dataSet.size(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; switch (viewType) { case Model.TEXT_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false); return new TextTypeViewHolder(view); case Model.IMAGE_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false); return new ImageTypeViewHolder(view); case Model.AUDIO_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_type, parent, false); return new AudioTypeViewHolder(view); } return null; } @Override public int getItemViewType(int position) { switch (dataSet.get(position).type) { case 0: return Model.TEXT_TYPE; case 1: return Model.IMAGE_TYPE; case 2: return Model.AUDIO_TYPE; default: return -1; } } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) { Model object = dataSet.get(listPosition); if (object != null) { switch (object.type) { case Model.TEXT_TYPE: ((TextTypeViewHolder) holder).txtType.setText(object.text); break; case Model.IMAGE_TYPE: ((ImageTypeViewHolder) holder).txtType.setText(object.text); ((ImageTypeViewHolder) holder).image.setImageResource(object.data); break; case Model.AUDIO_TYPE: ((AudioTypeViewHolder) holder).txtType.setText(object.text); } } } @Override public int getItemCount() { return dataSet.size(); } } 

For the reference link: Android RecyclerView Example – Multiple ViewTypes

Source Link
Sayan Manna
  • 699
  • 8
  • 13

Yes, it is possible. In your adapter getItemViewType Layout like this ....

 public class MultiViewTypeAdapter extends RecyclerView.Adapter { private ArrayList<Model>dataSet; Context mContext; int total_types; MediaPlayer mPlayer; private boolean fabStateVolume = false; public static class TextTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; CardView cardView; public TextTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.cardView = (CardView) itemView.findViewById(R.id.card_view); } } public static class ImageTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; ImageView image; public ImageTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.image = (ImageView) itemView.findViewById(R.id.background); } } public static class AudioTypeViewHolder extends RecyclerView.ViewHolder { TextView txtType; FloatingActionButton fab; public AudioTypeViewHolder(View itemView) { super(itemView); this.txtType = (TextView) itemView.findViewById(R.id.type); this.fab = (FloatingActionButton) itemView.findViewById(R.id.fab); } } public MultiViewTypeAdapter(ArrayList<Model>data, Context context) { this.dataSet = data; this.mContext = context; total_types = dataSet.size(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; switch (viewType) { case Model.TEXT_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false); return new TextTypeViewHolder(view); case Model.IMAGE_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false); return new ImageTypeViewHolder(view); case Model.AUDIO_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_type, parent, false); return new AudioTypeViewHolder(view); } return null; } @Override public int getItemViewType(int position) { switch (dataSet.get(position).type) { case 0: return Model.TEXT_TYPE; case 1: return Model.IMAGE_TYPE; case 2: return Model.AUDIO_TYPE; default: return -1; } } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) { Model object = dataSet.get(listPosition); if (object != null) { switch (object.type) { case Model.TEXT_TYPE: ((TextTypeViewHolder) holder).txtType.setText(object.text); break; case Model.IMAGE_TYPE: ((ImageTypeViewHolder) holder).txtType.setText(object.text); ((ImageTypeViewHolder) holder).image.setImageResource(object.data); break; case Model.AUDIO_TYPE: ((AudioTypeViewHolder) holder).txtType.setText(object.text); } } } @Override public int getItemCount() { return dataSet.size(); } } 

for reference link : https://www.journaldev.com/12372/android-recyclerview-example