问题:
我们有三个不同类型的对象:Book ,Magazine和Newspaper.我们想只使用一个List就能展示它们,同时在RecyclerView中的展示也不一样。
我们所需要的:
- 需要Book ,Magazine和Newspaper实现的一个接口。
- 一个RecyclerView.Adapter和一个可以强转为不同类型的接口列表。
- 每种类型对应的ViewHolder和布局文件。
接口
首先,我们创建一个所有类型要实现的接口Literature.这个接口会有方法getType()和表示不同类型的常量。
public interface Literature {
int TYPE_BOOK = 101;
int TYPE_MAGAZINE = 102;
int TYPE_NEWSPAPER = 103;
int getType();
}
此时,每个类型都去实现接口Literature。重置getType()方法返回该对象的正确类型。
public class Book implements Literature {
// variables and methods
@Override
public int getType() {
return Literature.TYPE_BOOK;
}
}
public class Magazine implements Literature{
// variables and methods
@Override
public int getType() {
return Literature.TYPE_MAGAZINE;
}
}
public class Newspaper implements Literature {
// variables and methods
@Override
public int getType() {
return Literature.TYPE_NEWSPAPER;
}
}
The RecyclerView.Adapter
首先,在Adapter里添加我们的Literature接口作为成员变量。
public class LiteratureAdapter extends RecyclerView.Adapter {
private List mLiteratureList;
}
我们同样也需要三个ViewHolder作为Adapter的内部类。一个对象一个内部类。
public class LiteratureAdapter extends RecyclerView.Adapter {
...
class BookViewHolder extends RecyclerView.ViewHolder {
public BookViewHolder(View itemView) {
super(itemView);
// get reference to views
// itemView.findViewById...
}
void bindView(int position) {
Book book = (Book) mLiteratureList.get(position);
// bind data to the views
// textView.setText()...
}
}
class MagazineViewHolder extends RecyclerView.ViewHolder {
public MagazineViewHolder(View itemView) {
super(itemView);
// get reference to views
}
void bindView(int position) {
Magazine magazine = (Magazine) mLiteratureList.get(position);
// bind data to the views
}
}
class NewspaperViewHolder extends RecyclerView.ViewHolder {
public NewspaperViewHolder(View itemView) {
super(itemView);
// get reference to views
}
void bindView(int position) {
Newspaper newspaper = (Newspaper) mLiteratureList.get(position);
// bind data to the views
}
}
}
我们需要重写RecyclerView.Adapter的四个方法。第一个就是getItemViewType()。此方法将会取出mLiteratureList里的一个条目,判断是否为一个Book, Magazine,或者Newspaper,然后将结果传给onCreateViewHolder()(此方法将会在后面实现)
@Override
public int getItemViewType(int position) {
return mLiteratureList.get(position).getType();
}
在onCreateViewHolder()中,我们会根据getItemViewType()传过来的类型创建一个ViewHolder
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case Literature.TYPE_BOOK:
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
return new BookViewHolder(itemView);
case Literature.TYPE_MAGAZINE:
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
return new MagazineViewHolder(itemView);
default: // TYPE_NEWSPAPER
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
return new NewspaperViewHolder(itemView);
}
}
在onBindViewHolder()中调用getItemViewType()。取决于返回的类型,将通用的ViewHolder强转为合适的类型,然后调用起bindView()方法。
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case Literature.TYPE_BOOK:
((BookViewHolder) holder).bindView(position);
break;
case Literature.TYPE_MAGAZINE:
((MagazineViewHolder) holder).bindView(position);
break;
case Literature.TYPE_NEWSPAPER:
((NewspaperViewHolder) holder).bindView(position);
break;
}
}
getItemCount()里的方法和以前标准的RecyclerView.Adapter的代码是一样的。
@Override
public int getItemCount() {
if (mLiteratureList == null) {
return 0;
} else {
return mLiteratureList.size();
}
}
设置数据
public void setLiteratureList(List extends Literature> literatureList) {
if (mLiteratureList == null){
mLiteratureList = new ArrayList<>();
}
mLiteratureList.clear();
mLiteratureList.addAll(literatureList);
notifyDataSetChanged();
}
好啦,点到为止。好好享受你的RecyclerView.Adapter吧。
翻译自:Display Objects of Different Types in a RecyclerView