onBindViewHolder 只执行第一个 ViewHolder
onBindViewHolder execute first ViewHolder only
我有三个 ViewHolders CardViewHolder
、TitleViewHolder
、GridViewHolder
,用于在用户选择特定布局时在它们之间切换,当我尝试切换到 Title 和网格 Layout/Views,我只看到我把它放在它的布局
中的 "Dummy title and default image"
这是我的 PostAdapter class:
public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Item> items;
public static final int CARD_LIST = 0;
public static final int CARD_MAGAZINE = 1;
public static final int TITLE = 2;
public static final int GRID = 3;
private int viewType;
PostAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
public void setViewType(int viewType) {
this.viewType = viewType;
notifyDataSetChanged();
}
public int getViewType() {
return this.viewType;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
if (this.viewType == CARD_LIST) {
view = inflater.inflate(R.layout.card_layout, parent, false);
return new CardViewHolder(view);
} else if (this.viewType == CARD_MAGAZINE) {
view = inflater.inflate(R.layout.card_magazine_layout, parent, false);
return new CardViewHolder(view);
} else if(this.viewType == TITLE){
view = inflater.inflate(R.layout.title_layout, parent, false);
return new TitleViewHolder(view);
}else {
view = inflater.inflate(R.layout.grid_layout,parent,false);
return new GridViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int itemType = getItemViewType(position);
final Item item = items.get(position);
final Document document = Jsoup.parse(item.getContent());
final Elements elements = document.select("img");
if (itemType == CARD_LIST || itemType == CARD_MAGAZINE) {
if (holder instanceof CardViewHolder) {
CardViewHolder cardViewHolder = (CardViewHolder) holder;
cardViewHolder.postTitle.setText(item.getTitle());
cardViewHolder.postDescription.setText(document.text());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(cardViewHolder.postImage);
} else if (itemType == TITLE) {
if (holder instanceof TitleViewHolder) {
TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
titleViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(titleViewHolder.postImage);
}
}else {
GridViewHolder gridViewHolder = (GridViewHolder) holder;
gridViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(gridViewHolder.postImage);
}
}
@Override
public int getItemCount() {
return items.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDescription;
public CardViewHolder(View itemView) {
super(itemView);
postImage = itemView.findViewById(R.id.postImage);
postTitle = itemView.findViewById(R.id.postTitle);
postDescription = itemView.findViewById(R.id.postDescription);
}
}
public class TitleViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public TitleViewHolder(@NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
public class GridViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public GridViewHolder(@NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
}
好像第一个ViewHolder只实现在第一个if block
,我试图通过日志查找问题的原因,但不幸的是我不知道为什么。
这是因为您没有通过覆盖 getItemViewType(int position)
方法来提供视图类型。您需要在 Adapter 中添加以下方法:
@Override
public int getItemViewType(int position) {
// this is just an example for returning the type.
// Assuming each item containing a type
return items.get(position).type;
// you can also use an if condition here
// for example
// if(position % 2 == 0) {
// return CARD_LIST;
// } else {
// return CARD_MAGAZINE;
// }
}
[已解决]
问题的发生是因为我正在检查通过方法 getItemViewType
获取的错误变量应该是这样的
int itemType = getViewType();
我有三个 ViewHolders CardViewHolder
、TitleViewHolder
、GridViewHolder
,用于在用户选择特定布局时在它们之间切换,当我尝试切换到 Title 和网格 Layout/Views,我只看到我把它放在它的布局
这是我的 PostAdapter class:
public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Item> items;
public static final int CARD_LIST = 0;
public static final int CARD_MAGAZINE = 1;
public static final int TITLE = 2;
public static final int GRID = 3;
private int viewType;
PostAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
public void setViewType(int viewType) {
this.viewType = viewType;
notifyDataSetChanged();
}
public int getViewType() {
return this.viewType;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
if (this.viewType == CARD_LIST) {
view = inflater.inflate(R.layout.card_layout, parent, false);
return new CardViewHolder(view);
} else if (this.viewType == CARD_MAGAZINE) {
view = inflater.inflate(R.layout.card_magazine_layout, parent, false);
return new CardViewHolder(view);
} else if(this.viewType == TITLE){
view = inflater.inflate(R.layout.title_layout, parent, false);
return new TitleViewHolder(view);
}else {
view = inflater.inflate(R.layout.grid_layout,parent,false);
return new GridViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int itemType = getItemViewType(position);
final Item item = items.get(position);
final Document document = Jsoup.parse(item.getContent());
final Elements elements = document.select("img");
if (itemType == CARD_LIST || itemType == CARD_MAGAZINE) {
if (holder instanceof CardViewHolder) {
CardViewHolder cardViewHolder = (CardViewHolder) holder;
cardViewHolder.postTitle.setText(item.getTitle());
cardViewHolder.postDescription.setText(document.text());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(cardViewHolder.postImage);
} else if (itemType == TITLE) {
if (holder instanceof TitleViewHolder) {
TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
titleViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(titleViewHolder.postImage);
}
}else {
GridViewHolder gridViewHolder = (GridViewHolder) holder;
gridViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(gridViewHolder.postImage);
}
}
@Override
public int getItemCount() {
return items.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDescription;
public CardViewHolder(View itemView) {
super(itemView);
postImage = itemView.findViewById(R.id.postImage);
postTitle = itemView.findViewById(R.id.postTitle);
postDescription = itemView.findViewById(R.id.postDescription);
}
}
public class TitleViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public TitleViewHolder(@NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
public class GridViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public GridViewHolder(@NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
}
好像第一个ViewHolder只实现在第一个if block
,我试图通过日志查找问题的原因,但不幸的是我不知道为什么。
这是因为您没有通过覆盖 getItemViewType(int position)
方法来提供视图类型。您需要在 Adapter 中添加以下方法:
@Override
public int getItemViewType(int position) {
// this is just an example for returning the type.
// Assuming each item containing a type
return items.get(position).type;
// you can also use an if condition here
// for example
// if(position % 2 == 0) {
// return CARD_LIST;
// } else {
// return CARD_MAGAZINE;
// }
}
[已解决]
问题的发生是因为我正在检查通过方法 getItemViewType
获取的错误变量应该是这样的
int itemType = getViewType();