我想知道如何创建一个包含多个视图持有者的回收站视图适配器,并且每次用该适配器实例化RecyclerView时,我只想查看特定类型,而不是混合类型。
我在Android活动中有2个不同的屏幕,每个屏幕都显示一个项目/卡片列表,一个是文本和数字,另一个是照片。现在,我为每个对象有2个不同的自定义适配器,而我想要实现的是一个适配器的灵活性,该适配器可以根据其显示的屏幕在对象类型之间进行延迟,并仅列出我需要的行。
我所看到的所有示例都是混合类型的对象,而这并不是我所追求的。
适配器代码附在下面。
rvalue
答案 0 :(得分:2)
public class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_REFERRAL_TRIGGER = 1;
private static final int VIEW_TYPE_NOTIFICATION = 2;
private static final int VIEW_TYPE_PROMOTION = 3;
private List<Object> notificationObjectsList;
private Activity mActivity;
public NotificationsAdapter(Activity activity, List<Object> mNotificationList) {
mActivity = activity;
notificationObjectsList = new ArrayList<>(mNotificationList);
}
// Used to avoid the duplicate items recycling
@Override
public long getItemId(int position) {
return position;
}
// Used to avoid the duplicate items recycling
@Override
public int getItemViewType(int position) {
if ((notificationObjectsList.get(position) != null) && (notificationObjectsList.get(position) instanceof ReferralData)) {
return VIEW_TYPE_REFERRAL_TRIGGER;
} else if (notificationObjectsList.get(position) instanceof Promotion) {
return VIEW_TYPE_PROMOTION;
} else {
return VIEW_TYPE_NOTIFICATION;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case VIEW_TYPE_REFERRAL_TRIGGER:
return new NotificationsAdapter.ReferralTriggerViewHolder(inflater.inflate(R.layout.row_referral_trigger, parent, false));
case VIEW_TYPE_NOTIFICATION:
return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
case VIEW_TYPE_PROMOTION:
return new NotificationsAdapter.PromotionViewHolder(inflater.inflate(R.layout.row_layout_app_promotions, parent, false));
default:
return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
if (viewHolder instanceof PromotionViewHolder) {
final PromotionViewHolder promotionViewHolder = (PromotionViewHolder) viewHolder;
//populate data of promotion view holder
} else if (viewHolder instanceof UserNotificationViewHolder) {
final UserNotificationViewHolder notificationViewHolder = (UserNotificationViewHolder) viewHolder;
//populate data of UserNotificationViewHolder view holder
} else if (viewHolder instanceof ReferralTriggerViewHolder) {
final ReferralTriggerViewHolder referralTriggerViewHolder = (ReferralTriggerViewHolder) viewHolder;
//populate data of ReferralTriggerViewHolder view holder
}
}
@Override
public int getItemCount() {
return notificationObjectsList.size();
}
private class UserNotificationViewHolder extends RecyclerView.ViewHolder {
private UserNotificationViewHolder(View itemView) {
super(itemView);
}
}
private class PromotionViewHolder extends RecyclerView.ViewHolder {
private PromotionViewHolder(View itemView) {
super(itemView);
}
}
private class ReferralTriggerViewHolder extends RecyclerView.ViewHolder {
ReferralTriggerView referralTriggerView;
private ReferralTriggerViewHolder(View itemView) {
super(itemView);
referralTriggerView = itemView.findViewById(R.id.rtv_notification_adapter);
}
}
}
答案 1 :(得分:0)
我不明白您的问题在哪里?您能否提供更多说明以实现您的目标?
//编辑
在您的代码中,您确定只有第一个项目将是VIEW0类型的项目,而其他项目将是VIEW1类型。这在getItemViewType(int position)
函数中。
如果要确定选择哪种ViewHolder,则必须先选择它,然后再填充列表。在适配器中创建枚举:
enum TYPE {
VIEW0,
VIEW1
}
然后创建枚举变量:
private TYPE itemType;
并将其传递给您的构造函数:
public Test(ArrayList<MyDataProvider> _arrayList, TYPE itemType) {
this.arrayList = new ArrayList<>();
this.arrayList = _arrayList;
this.itemType = itemType;
}
,然后在itemType
函数中检查您的类型是什么(基于onCreateViewHolder
变量)。请注意,switch中的变量是“ itemType”,而不是覆盖方法中的viewType。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (itemType) {
case VIEW0:
return new NotificationsAdapter.ReferralTriggerViewHolder(inflater.inflate(R.layout.row_referral_trigger, parent, false));
case VIEW1:
return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
default:
return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
}
}