其他分享
首页 > 其他分享> > android – RecyclerView onCreateViewHolder返回类型与多个自定义ViewHolders不兼容

android – RecyclerView onCreateViewHolder返回类型与多个自定义ViewHolders不兼容

作者:互联网

我正在尝试在RecyclerView中使用多个ViewHolders,以便在运行时交换这些视图.我创建了两个扩展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();
    }
}

它们在我的RecyclerViewAdapter类的onCreateViewHolder函数中调用:

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;
          }
    }

当我尝试运行代码时,我收到此错误:

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

所以,问题似乎是onCreateViewHolder不接受我的自定义类继承自RecyclerView.ViewHolder.我正在使用Android Studio,所以问题就在那里.我很困惑,因为我认为Java继承可以毫无问题地处理这个问题.有人可以提出解决方案吗?

编辑:当我将onCreateViewHolder的返回类型更改为只有一个VH的MenuItemViewHolder时,一切正常.但是,当我有多个可以随时交换的VH类时,这不起作用.

解决方法:

RecyclerView.Adapter是一个参数化类,它采用单个ViewHolder类型.您的定义可能如下所示:

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

它应该是:

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

不要忘记为onBindViewHolder定义正确的定义.您还需要覆盖getItemViewType.

标签:android-viewholder,android,android-recyclerview,recyclerview-layout,staggeredgri
来源: https://codeday.me/bug/20191001/1840305.html