RecyclerView.Adapter的onCreateViewHolder和onBindViewHolder方法未得到调用 [英] RecyclerView.Adapter's onCreateViewHolder and onBindViewHolder methods are not getting called
问题描述
我正在按照我的项目中提供的示例项目,尝试在我的项目中实现 TwoWayView 该链接.我已经实现了所有内容,并且我的代码可以正常运行,没有任何错误,但是实际的List并没有膨胀.调试后,我发现适配器设置正确,并且getItemCount
方法也返回正数,但是未调用其他两个覆盖的方法onCreateViewHolder
和onBindViewHolder
.我不知道为什么它不起作用.请查看下面的部分代码,我们将为您提供任何帮助.谢谢.
I'm trying to implement TwoWayView in my project by following the sample project provided in that link. I have implemented everything and my code works without any errors but the actual List is not getting inflated. Upon debugging I found out that the adapter is set correctly and the getItemCount
method is also returning positive count but the other two overridden methods onCreateViewHolder
and onBindViewHolder
are not getting called. I have no clue why it is not working. Please see the partial code below, any help is appreciated. Thanks.
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
内,并且getItemCount
返回的是正数.计数.
Note: I have gone through similar questions but the answers provided there are not relevant to my question because my RecyclerView
is not inside ScrollView
and also the getItemCount
is returning the positive count.
推荐答案
我认为您忘记了回收站视图中的setLayoutManager(LayoutManager)
.如果没有布局管理器,则仅调用getItemCount()
.
I think you forgot the setLayoutManager(LayoutManager)
on the recycler view. Without Layout Manager, only getItemCount()
is called.
尝试使用yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
这篇关于RecyclerView.Adapter的onCreateViewHolder和onBindViewHolder方法未得到调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!