我正在尝试按照该链接中提供的示例项目在我的项目中实现TwoWayView。我实现了一切,我的代码工作没有任何错误,但实际的List没有得到膨胀。在调试时,我发现适配器设置正确,getItemCount
方法也返回正数,但其他两个重写方法onCreateViewHolder
和onBindViewHolder
未被调用。我不知道为什么它不起作用。请参阅下面的部分代码,任何帮助表示赞赏。感谢。
MyAdapter.java 类
public class MyAdapter extends RecyclerView.Adapter < MyViewHolder > {
private MySimpleCursorAdapter mCursor;
//some other private fields here
public MyAdapter(Context context, Cursor c, int resourceId, ViewType viewType, String containerId) {
mCursor = new MySimpleCursorAdapter(a1,a2,a3,a4,a5,a6); //Passed necessary arguments
....
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.w("onCreateViewHolder", "--------->executed"); //Line not executed
final View view = mCursor.newView(mContext, mCursor.getCursor(), parent);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.w("onBindViewHolder", "--------->executed"); //Line not executed
mCursor.bindView(holder.itemView, mContext, mCursor.getCursor());
}
@Override
public int getItemCount() {
Log.w("count", Integer.toString(mCursor.getCount())); //This is executed
return mCursor.getCount();
}
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(mLayoutToInflate, null);
....
return view;
}
@Override
public void bindView(final View view, Context context, Cursor cursor) {
//Implemented this method for inflating all subviews inside each item of the list
}
}
}
注意:我已经遇到了类似的问题,但我提供的答案与我的问题无关,因为我的RecyclerView
不在ScrollView
内,而且{{1返回正数。
答案 0 :(得分:9)
我想你忘记了回收者视图上的setLayoutManager(LayoutManager)
。如果没有布局管理器,则仅调用getItemCount()
。
尝试使用yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
答案 1 :(得分:0)
首先,在实例化适配器时,应该保留对Context的引用:
public class MyAdapter extends RecyclerView.Adapter < MyViewHolder > {
private MySimpleCursorAdapter mCursor;
private Context context; // <-- add this line
//some other private fields here
public MyAdapter(Context context, Cursor c, int resourceId, ViewType viewType, String containerId) {
mCursor = new MySimpleCursorAdapter(a1,a2,a3,a4,a5,a6);
this.context = context; // <-- add this line
//Passed necessary arguments
....
}
然后,在onCreateViewHolder方法中,使用对Context的引用来为ViewHolder创建视图:
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.w("onCreateViewHolder", "--------->executed"); //Line not executed
final View view = mCursor.newView(context, mCursor.getCursor(), parent);
return new MyViewHolder(view);
}
然后在CursorAdapter中按如下方式对视图进行充气:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(mLayoutToInflate, parent, false);
return view;
}
我遇到了同样的问题,虽然我使用JSON调用来检索数据而不是Cursor Adapter,但这些更改解决了问题。