How to set multiple RecycleView to a single adapter in Firebase?
I am creating an app, there are a lot of categories in my app, and creating multiple adapters, models, and RecycleView makes an app complicated. It is hard to make different adapters for multiple RecycleView.
Can I set multiple RecycleView to one single adapter? By using if or else or switch cases how to do this?
Or is there any alternative?
First Model Class
public class FirstModel {
private String Image, Text;
FirstModel() {
}
public FirstModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
}
Second Model Class
public class SecondModel {
private String Image, Text;
SecondModel() {
}
public SecondModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
Link To One Adapter
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int FirstLayout = 0;
private final int SecondLayout = 1;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == FirstLayout) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHerViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHimViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
????
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 1:
?????
}
}
@Override
public int getItemCount () {
return ???? .size();
}
class FirstViewHolder extends RecyclerView.ViewHolder {
private ImageView Image;
private TextView Text;
public FirstViewHolder(@NonNull View itemView) {
super(itemView);
Image = (ImageView) itemView.findViewById(R.id.Image);
Text = (TextView) itemView.findViewById(R.id.Text);
}
}
class SecondViewHolder extends RecyclerView.ViewHolder {
public SecondViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}