首页 > 解决方案 > 如何使 onBindViewHolder 在单个适配器中允许不同对象类型的列表?

问题描述

我想让 android RecyclerView.Adapter 允许在单个适配器中列出不同的对象类型。
有两个数据 DTO:product 和 post。
涉及多个视图类型时如何分页?
当我像下面这样制作时,它会出现错误“Product product = getItem(position);”。
我不想继承我的 DTO。

在此处输入图像描述

PostsRecyclerAdapter.class

 public class PostsRecyclerAdapter extends PagedListAdapter<Post, RecyclerView.ViewHolder> {
    private static final int LAYOUT_DETAIL_PRODUCT = 0;
    private static final int LAYOUT_POSTS = 1;

                               ...

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


        switch (getItemViewType(position)) {
            case LAYOUT_DETAIL_PRODUCT:
                Product product = getItem(position); **=> get error!**
                DetailProductViewHolder detailProductViewHolder = (DetailProductViewHolder) holder;
                detailProductViewHolder.detailProductName.setText(product.getPrTitle());
                detailProductViewHolder.detailProductRating.setText(Integer.toString(product.getPrScore()));
                loadingPicture(product.getPrImage()).into(detailProductViewHolder.detailProductImage);
                break;

            case LAYOUT_POSTS:
                Post post = getItem(position);
                PostsViewHolder postsViewHolder = (PostsViewHolder) holder;
                postsViewHolder.postGoodPoint.setText(post.getGoodContents());
                postsViewHolder.postBadPoint.setText(post.getBadContents());
                loadingPicture(post.getPrImage()).into(postsViewHolder.postPicture);
                break;   
        }
    }

课后

      public class Post {
          @SerializedName("id")
          private Integer id;
          @SerializedName("title")
          private String title;
          @SerializedName("score")
          private Integer score;     
          private String storedPath;
          @SerializedName("good_contents")
          private String goodContents;
          @SerializedName("bad_contents")
          private String badContents;
          @SerializedName("pr_id")
          private Integer prId;


   public Integer getId() {
       return id;
  }
   public void setId(Integer id) {
       this.id = id;
   }
   ...
 }

产品类

public class Product {
    @SerializedName("pr_id")
    @Expose
    private Integer prId;
    @SerializedName("pr_title")
    @Expose
    private String prTitle;
    @SerializedName("pr_score")
    @Expose
    private Integer prScore;
    @SerializedName("pr_registed_time")
    @Expose
    private String prRegistedTime;
    @SerializedName("pr_image")
    @Expose
    private String prImage;
    @SerializedName("pr_category")
    @Expose
    private Integer prCategory;
    @SerializedName("pr_review_count")
    @Expose
    private Integer prReviewCount;

    public Product(String productPicture, Integer productCategory, String title, int reviewCount, int productScore) {
        prImage = productPicture;
        prCategory = productCategory;
        prTitle = title;
        prReviewCount = checkReviewCount(reviewCount);
        prScore = productScore;
    }
....
}

标签: androidandroid-recyclerview

解决方案


我会使用一个空接口并在 Post 和 Product 上实现它(我们称之为 MyInterface)并替换你的PostsRecyclerAdapter类上的类型

public class PostsRecyclerAdapter extends PagedListAdapter<MyInterface, RecyclerView.ViewHolder>

在您的适配器中,您应该转换为适当的情况:

Product product = (Product) getItem(position)
.
.
.
Post post = (Post) getItem(position);

推荐阅读