I'm trying to use multiple ViewHolders in a RecyclerView in order to swap these views out at run time. I have created two classes which extend RecyclerView.ViewHolder:
MenuItemViewHolder
public class MenuItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView menuItemText;
public ImageView menuItemPhoto;
public RecyclerViewAdapter rva;
public MenuItemViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
rva = caller;
menuItemText = (TextView) itemView.findViewById(R.id.grid_text);
menuItemPhoto = (ImageView) itemView.findViewById(R.id.grid_image);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
SelectedItemViewHolder
public class SelectedItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView menuItemText;
public ImageView menuItemPhoto;
public RecyclerViewAdapter rva;
public SelectedItemViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
menuItemText = (TextView) itemView.findViewById(R.id.grid_text_full);
menuItemPhoto = (ImageView) itemView.findViewById(R.id.grid_image_full);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
They are called within my RecyclerViewAdapter class's onCreateViewHolder function:
RecyclerViewAdapter onCreateViewHolder
// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
View gridItemLayout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.grid_single, parent, false);
// set the view's size, margins, paddings and layout parameters
MenuItemViewHolder vh = new MenuItemViewHolder(gridItemLayout);
case 2:
View selectedItemLayout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.grid_single_full, parent, false);
// set the view's size, margins, paddings and layout parameters
SelectedItemViewHolder sivh = new SelectedItemViewHolder(selectedItemLayout);
return sivh;
}
}
When I try to run the code, I get this error:
Error:(48, 36) error: onCreateViewHolder(ViewGroup,int) in RecyclerViewAdapter cannot override onCreateViewHolder(ViewGroup,int) in Adapter return type ViewHolder is not compatible with MenuItemViewHolder where VH is a type-variable: VH extends ViewHolder declared in class Adapter
So, the issue seems to be that onCreateViewHolder doesn't accept my custom classes as being inherited from RecyclerView.ViewHolder. I'm using Android Studio, so perhaps the issue is there. I'm perplexed since I thought Java inheritance would handle this with no problems. Could someone please propose a solution?
Edit: When I change the return type of onCreateViewHolder to MenuItemViewHolder with only one VH, things work fine. However, this doesn't work when I have multiple VH classes which can be swapped at any time.
RecyclerViewAdapter
? – ianhanniballake