21

我试图在 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;
          }
    }

当我尝试运行代码时,出现此错误:

错误:(48, 36) 错误:RecyclerViewAdapter 中的 onCreateViewHolder(ViewGroup,int) 无法覆盖 Adapter 返回类型中的 onCreateViewHolder(ViewGroup,int) ViewHolder 与 VH 是类型变量的 MenuItemViewHolder 不兼容:VH 扩展了在类 Adapter 中声明的 ViewHolder

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

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

4

2 回答 2

30

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

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

它应该是:

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

不要忘记也有正确的定义onBindViewHolder。您还需要覆盖getItemViewType

于 2015-08-17T00:08:52.360 回答
3

完整的工作代码

对于那些想要完整工作代码的人。你去吧。

 public class YourListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

      private static final int LAYOUT_ONE= 0;
      private static final int LAYOUT_TWO= 1;

      @Override
      public int getItemViewType(int position)
      {
          if(position==0)
             return LAYOUT_ONE;
          else
             return LAYOUT_TWO;
      }

      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

         View view =null;
         RecyclerView.ViewHolder viewHolder = null;

         if(viewType==LAYOUT_ONE)
         {
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one,parent,false);
            viewHolder = new ViewHolderOne(view);
         }
         else
         {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.two,parent,false);
            viewHolder= new ViewHolderTwo(view);
         }

        return viewHolder;

      }

      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

          if(holder.getItemViewType()== LAYOUT_ONE)
          {
              // Typecast Viewholder 
              // Set Viewholder properties 
              // Add any click listener if any 
          }
          else {

              ViewHolderOne vaultItemHolder = (ViewHolderOne) holder;
              vaultItemHolder.name.setText(displayText);
              vaultItemHolder.name.setOnClickListener(new View.OnClickListener() {

                     @Override
                     public void onClick(View v) {
                      .......
                     }
                });

            }

        }

     //****************  VIEW HOLDER 1 ******************//

      public class ViewHolderOne extends RecyclerView.ViewHolder {

          public TextView name;

          public ViewHolderOne(View itemView) {
               super(itemView);
               name = (TextView)itemView.findViewById(R.id.displayName);
          }
      }


   //****************  VIEW HOLDER 2 ******************//

      public class ViewHolderTwo extends RecyclerView.ViewHolder{

          public ViewHolderTwo(View itemView) {
          super(itemView);

           ..... Do something
      }
   }
}

阅读更多

您可以阅读有关使用多个 ViewHolders 创建 RecyclerView 的更多信息

于 2018-12-22T09:22:40.723 回答