RecyclerView.Adapter的onCreateViewHolder和onBindViewHolder方法没有被调用。

4
我正在尝试通过遵循提供在TwoWayView链接中的示例项目来实现TwoWayView。我已经实现了所有内容,我的代码没有任何错误,但实际的列表没有被填充。通过调试,我发现适配器被正确设置,并且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内,并且getItemCount返回正数计数。


请使用此适配器 https://gist.github.com/Shywim/127f207e7248fe48400b - pskink
2个回答

9

我认为您忘记在回收视图上设置setLayoutManager(LayoutManager)。没有布局管理器,只有getItemCount()被调用。

请尝试使用yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


嘿,谢谢回复,但我没有使用默认的_LinearLayoutManager_。如果您查看示例代码此处(第128行),他也没有使用_setLayoutManager_。 - Prudhvi
你是否将 app:twowayview_layoutManager="ListLayoutManager"(或其他layoutmanager)放入了xml文件中,就像这个链接中的例子 https://github.com/lucasr/twoway-view/blob/master/sample/src/main/res/layout/layout_list.xml#L25 - xiaomi
是的,我把它放在我的 XML 文件中了。 - Prudhvi

0

首先,在适配器实例化时,您应该保留对上下文的引用:

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,这些更改解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接