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

Pru*_*hvi 4 android simplecursoradapter android-viewholder android-recyclerview

我正在尝试通过遵循该链接中提供的示例项目在我的项目中实现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
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

注意:我已经经历了类似的问题,但是那里提供的答案与我的问题无关,因为我RecyclerView不在内部ScrollView,而且getItemCount正在返回正数.

xia*_*omi 9

我想你忘记setLayoutManager(LayoutManager)了回收者的观点了.没有布局管理器,只会getItemCount()被调用.

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