android - 如何使 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;
}
....
}
解决方案
我会使用一个空接口并在 Post 和 Product 上实现它(我们称之为 MyInterface)并替换你的PostsRecyclerAdapter
类上的类型
public class PostsRecyclerAdapter extends PagedListAdapter<MyInterface, RecyclerView.ViewHolder>
在您的适配器中,您应该转换为适当的情况:
Product product = (Product) getItem(position)
.
.
.
Post post = (Post) getItem(position);
推荐阅读
- javascript - 如何在 Chrome Devtools JS 控制台中从“http://bar.org/some-esm-module.js”导入 blah?
- javascript - Uncaught (in promise) TypeError: existingData.push is not a function
- javascript - 当数据更新改变字体颜色
- algorithm - 最大产品切割算法
- python-3.x - 使用 tkinter 创建按钮的问题
- c# - 使用移动 AWS 开发工具包 (Unity) 和 DynamoDB 进行批量操作
- java - CoreException:无法计算构建计划:插件 org.apache.maven.plugins:maven-compiler-plugin:2.5.1 或其依赖项之一
- javascript - 使用 R 更改页面中的下拉列表
- arrays - 是否可以在 C 中创建动态指针数组?
- jmeter - 如何配置 Jmeter 以将相同的数据写入 csv/xml,因为它在 GUI 的摘要报告中显示?