所以基本上我有两个不同的CardView,一旦按下它们就想切换,因此我创建了ViewHolder作为这两个超类的持有者,即MaterialCardView。 当我尝试验证收到的卡时,我在ViewHolder上使用instanceof。之后,我将CardView视为其原始类。但是,一旦这样做,就会出现Java错误“类型参数T具有不兼容的上限:视图和字符串”。
我尝试过使缓存和重构无效的解决方案,这是网站上的主要解决方案。不幸的是,这似乎是一个Java问题。
这是我的VH(到目前为止):
public class ProjectItemHolder extends RecyclerView.ViewHolder {
MaterialCardView projectItem;
public ProjectItemHolder(MaterialCardView projectItem) {
super(projectItem);
if (projectItem instanceof unselectedCard) {
((unselectedCard) projectItem).imageUrl = itemView.findViewById(R.id.thumbnail);
((unselectedCard)projectItem).projectName = itemView.findViewById(R.id.projectName);
((unselectedCard)projectItem).projectDescription = itemView.findViewById(R.id.projectDescription);
我的unselectedCard类:
public class unselectedCard extends MaterialCardView {
String projectName;
String projectDescription;
String imageUrl;
public unselectedCard(Context context,String projectName, String projectDescription, String imageUrl) {
super(context);
this.projectName = projectName;
this.projectDescription = projectDescription;
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public String getProjectDescription() {
return projectDescription;
}
public String getProjectName() {
return projectName;
}
public void copyAnotherCard(unselectedCard card) {
this.projectName = card.getProjectName();
this.projectDescription = card.getProjectDescription();
this.imageUrl = card.getImageUrl();
}
和我要协调的其他Cardview:
public class selectedCard extends MaterialCardView {
private String projectName,projectDescription,managerName;
private String thumbnailUrl,imageUrl;
public selectedCard(@NonNull Context context, String projectName, String projectDescription, String managerName, String thumbnailUrl, String imageUrl) {
super(context);
this.projectName = projectName;
this.projectDescription = projectDescription;
this.managerName = managerName;
this.thumbnailUrl = thumbnailUrl;
this.imageUrl = imageUrl;
}
public String getProjectName() {
return projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public String getImageUrl() {
return imageUrl;
}
public String getManagerName() {
return managerName;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void copyAnotherCard(selectedCard card) {
this.projectName = card.getProjectName();
this.projectDescription = card.getProjectDescription();
this.managerName = card.getManagerName();
this.thumbnailUrl = card.getThumbnailUrl();
this.imageUrl = card.getImageUrl();
}
我希望有人能告诉我如何解决此问题。
谢谢!