【发布时间】:2018-09-28 15:28:01
【问题描述】:
我已经实现了一个 RecyclerView 如下
带支架的适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.indovinelloelement,parent,false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
holder.category.setText(data.get(position).getCategory());
holder.difficulty.setText(""+data.get(position).getDifficulty());
holder.title.setText(data.get(position).getTitle());
holder.score.setText(""+data.get(position).getScore());
if(data.get(position).isLocked())
holder.setLocked();
else
holder.setUnlocked();
}
public DataGestour.Indovinello getIndovinello(int pos){
return data.get(pos);
}
@Override
public int getItemCount() {
return data.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
public TextView category,score,difficulty,title;
private ImageView lock_state;
public MyHolder(View itemView) {
super(itemView);
category = (TextView)itemView.findViewById(R.id.categoria);
score = (TextView)itemView.findViewById(R.id.punteggio);
difficulty = (TextView) itemView.findViewById(R.id.difficolta);
title = (TextView)itemView.findViewById(R.id.titolo);
lock_state = (ImageView) itemView.findViewById(R.id.lock_state);
}
public void setLocked(){
lock_state.setImageResource(R.drawable.ic_lock_outline_black_24dp);
}
public void setUnlocked(){
lock_state.setImageResource(R.drawable.ic_lock_open_black_24dp);
}
}
ArrayList<DataGestour.Indovinello> data;
Context context;
public MyAdapter(Context context, ArrayList<DataGestour.Indovinello> list){
this.context = context;
this.data = list;
}
}
和 RecyclerView:
rc = (RecyclerView)root_view.findViewById(R.id.lista_domande);
rc.setHasFixedSize(true);
LinearLayoutManager ly = new LinearLayoutManager(getContext());
ly.setOrientation(LinearLayoutManager.VERTICAL);
rc.setLayoutManager(ly);
try{
gestour = new DataGestour(getContext());
}catch (IllegalStateException e){
Log.e("DataManager",e.getMessage());
};
adapter = new MyAdapter(getContext(),gestour.getAllDatas());
rc.setAdapter(adapter);
adapter.notifyDataSetChanged();
其中 DataGestoure 是一个从 数据库并将它们存储在ArrayList中(DataGestour.getAllDatas是返回ArrayList下数据的方法)
只有当 ArrayList 包含超过 3 个项目时才会出现问题,因为尽管适配器将所有数据保存在 ArrayList data 中,但 RecyclerView 不显示它们
【问题讨论】:
-
您是否尝试在适配器的 getItemCount() 方法中设置断点?检查您是否在此处返回 3 或数据的实际大小。和/或在 onBindViewHolder() 中设置断点以查看它被调用的频率。还可以尝试上下滑动您的列表......也许它只是显示列表的布局没有完全显示。
-
在提问之前尝试过。滚动列表不起作用 onBindViewHolder 调用了 3 次,getItemCount 返回 8
标签: java android android-recyclerview