4

我试图在我的项目中执行TwoWayView,方法是按照该链接中提供的示例项目。我已经实现了一切,我的代码工作没有任何错误,但实际列表没有得到充气。经过调试,我发现适配器设置正确,getItemCount方法也返回正数,但其他两个重写的方法onCreateViewHolderonBindViewHolder没有被调用。我不知道为什么它不工作。请参阅下面的部分代码,任何帮助表示赞赏。谢谢。RecyclerView.Adapter的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是有不相关的我的问题返回正数。

+0

使用此适配器https://gist.github.com/Shywim/127f207e7248fe48400b – pskink

回答

9

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

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

+0

嘿,谢谢你的回复,但我没有使用默认_LinearLayoutManager_。如果你看到示例代码[这里](https://github.com/lucasr/twoway-view/blob/master/sample/src/main/java/org/lucasr/twowayview/sample/LayoutFragment.java)(line 128)他也没有使用_setLayoutManager_。 – Prudhvi

+0

所以你把它放到xml文件'app:twowayview_layoutManager =“ListLayoutManager”'? (或任何layoutmanager)像这样https://github.com/lucasr/twoway-view/blob/master/sample/src/main/res/layout/layout_list.xml#L25 – xiaomi

+0

是的,我把它放在我的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方法,使用参考上下文来创建视图在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调用来检索数据而不是光标适配器,并且这些更改解决了问题。

相关问题