我试图创建recyclelerview,它显示来自一个对象(ObjectMaster)的两个不同数组的结果。只要两个数组都显示在列表中,我就不在乎订单了。任何想法或建议将非常感激。
public ObjectMaster {
List<Object1> first_objects;
List<Object2> second_objects;
}
public class ObjectMasterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ObjectMaster object;
private static final int TYPE_OBJECT1 = 0;
private static final int TYPE_OBJECT2 = 1;
public ObjectMasterAdapter(ObjectMaster object) {
this.object = object;
}
public class Object1Holder extends RecyclerView.ViewHolder {
private TextView title;
private TextView subtitle;
public Object1Holder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
subtitle = (TextView) itemView.findViewById(R.id.subtitle);
}
public void bindObj(Object1 object) {
title.setText(object.getTitle());
subtitle.setText(object.getSubtitle());
}
}
public class Object2Holder extends RecyclerView.ViewHolder {
private TextView title;
public Object2Holder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
public void bindObj(Object2 object) {
title.setText(object.getTitle());
}
}
@Override
public int getItemViewType(int position) {
// I need to figure out the best way to determine if its already listed objects from the first object array (Object1)
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.object_item, viewGroup, false);
switch(i) {
case TYPE_OBJECT1: return new Object1Holder(view);
case TYPE_OBJECT2: return new Object2Holder(view);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
switch (viewHolder.getItemViewType()) {
case TYPE_OBJECT1:
Object1Holder object1 = (Object1Holder) viewHolder;
object1.bindObj(object.first_objects.get(i));
break;
case TYPE_OBJECT2:
Object2Holder object2 = (Object2Holder) viewHolder;
object2.bindObj(object.second_objects.get(i));
break;
}
}
@Override
public int getItemCount() {
return object.first_objects.size() + object.second_objects.size();
}
答案 0 :(得分:4)
我需要弄清楚确定它是否已经列出来自第一个对象数组(Object1)的对象的最佳方法
如果position
大于(或等于)第一个数组的长度,则现在进入第二个数组。从position
减去第一个数组的长度,使索引进入第二个数组。
另请注意,您在onBindViewHolder()
中有一个错误,因为您没有执行类似的减法来将position
(i
值)更改为您的第二个数组中的索引,当它是您正在处理的第二个数组视图类型时。