案例一:
显示 productName
的列表在 View
发送时 List<Products> productList
在适配器构造函数中。
案例二:
显示 categoryName
的列表在 View
发送时 List<Category> categoryList
在适配器构造函数中。
两种情况必须使用相同的 RecyclerView
适配器。
适配器可能有 List<Object> objectList
.但是如何在 View
中显示不同对象的不同属性?
最佳答案
检查类型:
@Override
public void onBindViewHolder(WhateverHolder holder, int position) {
Object item = itemsList.get(position);
if (item instanceof Products) {
Product product = (Product) item;
//populate the ViewHolder with the information
} else if (item instanceof Category) {
Category category = (Category) item;
//populate the ViewHolder
} else {
throw IllegalArgumentException("Invalid item: " + item.getClass().getCanonicalName()); //this is optional
}
}
关于android - 是否可以创建一个动态的 RecyclerView 适配器来接受任何对象列表 <Object> 的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52705163/