所以我有这两个arraylists:
ArrayList<String> a = new ArrayList();
ArrayList<String> b = new ArrayList();
a.add("bear","lion","bird","goose","cat");
b.add("car","bike");
然后我将它们添加到一个新的arraylist:
ArrayList<ArrayList<String>> list = new ArrayList();
list.add(a);
list.add(b);
然后,我将此arraylist设置为customlerview的自定义适配器。在这个适配器中,我还将这两个arraylists(在最后一个arraylist中)设置为recyclerview,它是适配器的项目视图。因此,它是另一个recyclerview的自定义适配器内的recyclerview。
然而,当我调试应用程序时,第一个包含5个项目的arraylist在recyclerview上只显示2个。像第二个arraylist。然后,当我删除第二个arraylist并再次调试应用程序时,第一个arraylist将显示其真实值。
如果我在recyclerview显示错误值后调用notifyDataSetChanged(),它将显示其真值。所以,我认为是因为某些视图(我不知道如果它是recyclelerview,片段或其他东西)还没有正确加载。
我该如何解决这个问题?谢谢。
修改 这是我的适配器代码:
public class MyAdapter extends RecyclerView.Adapter<AdapterNearMe.MyViewHolder> {
private Context context;
private List<List<String>> list;
public class MyViewHolder extends RecyclerView.ViewHolder {
public RecyclerView rView;
public MyViewHolder(View view) {
super(view);
rView = (RecyclerView) view.findViewById(R.id.rview);
}
}
public MyAdapter (Context context, List<List<String>> list) {
this.context= context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_near_me, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final List<String> newList= list.get(position);
holder.rView.setAdapter(new AdapterRView(context,newList));
holder.rView.setLayoutManager(new GridLayoutManager(context,3));
}
@Override
public int getItemCount() {
return list.size();
}
}
答案 0 :(得分:0)
RecyclerView's
bindViewHolder
为您提供了在单元格view
上设置属性的功能,而不是为父adapter
设置RecylerView
。
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
// This should single Model rather than List
final List<Model> newList= list.get(position);
// Instead of creating another recyclerview, check how to create section view
holder.rView.setAdapter(new AdapterRView(context,newList));
holder.rView.setLayoutManager(new GridLayoutManager(context,3));
}
答案 1 :(得分:0)
如何在onCreateViewHolder()方法中设置适用于recyclerview的适配器(带空arraylist)?然后用真实数据填充它,然后在onBindViewHolder()中调用notifyDataSetChange()?
public class MyAdapter extends RecyclerView.Adapter<AdapterNearMe.MyViewHolder> {
private Context context;
private List<List<String>> list;
public class MyViewHolder extends RecyclerView.ViewHolder {
public RecyclerView rView;
public MyViewHolder(View view) {
super(view);
rView = (RecyclerView) view.findViewById(R.id.rview);
rView.setLayoutManager(new GridLayoutManager(context,3));
}
}
public MyAdapter (Context context, List<List<String>> list) {
this.context= context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_near_me, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
holder.rView.setAdapter(new AdapterRView(context, new ArrayList<String>));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final List<String> newList = list.get(position);
AdapterRView adapter = holder.rView.getAdapter();
if (adapter != null) {
adapter.swapList(newList);
adapter.notifyDataSetChanged();
}
}
@Override
public int getItemCount() {
return list.size();
}
}
您需要添加一个额外的方法来交换AdapterRView.class的列表:
public class AdapterRView.class {
....
private List<String> list;
public void swapList(List<String> list) {
this.list = list;
}
}