Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

android - RecyclerView onCreateViewHolder Return Type Incompatibility With Multiple Custom ViewHolders

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The RecyclerView.Adapter is a parameterized class that takes in a single ViewHolder type. Your definition probably looks like:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MenuItemViewHolder>

It should be:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>

Don't forget to have the right definition for onBindViewHolder as well. You need to override getItemViewType as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...