【问题标题】:RecyclerView.Adapter's onCreateViewHolder and onBindViewHolder methods are not getting calledRecyclerView.Adapter 的 onCreateViewHolder 和 onBindViewHolder 方法没有被调用
【发布时间】:2015-09-08 16:54:06
【问题描述】:

我正在尝试按照该链接中提供的示例项目在我的项目中实施TwoWayView。我已经实现了一切,我的代码没有任何错误,但实际的 List 并没有膨胀。经过调试,我发现适配器设置正确,getItemCount 方法也返回正数,但其他两个被覆盖的方法onCreateViewHolderonBindViewHolder 没有被调用。我不知道为什么它不起作用。请参阅下面的部分代码,不胜感激。谢谢。

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 正在返回正面数。

【问题讨论】:

标签: android simplecursoradapter android-recyclerview android-viewholder


【解决方案1】:

我想你忘记了回收站视图上的setLayoutManager(LayoutManager)。没有布局管理器,只会调用getItemCount()

试试yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

【讨论】:

  • 您好,感谢您的回复,但我没有使用默认的 LinearLayoutManager。如果您看到示例代码 here(第 128 行),他也没有使用 setLayoutManager
  • 那么你把app:twowayview_layoutManager="ListLayoutManager" 放入xml文件了吗? (或任何布局管理器)像这样github.com/lucasr/twoway-view/blob/master/sample/src/main/res/…
  • 是的,我把它放在了我的 XML 文件中。
【解决方案2】:

首先,您应该在实例化 Adapter 时保留对 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 调用而不是光标适配器来检索数据,这些更改解决了问题。

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多