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調用來檢索數據而不是光標適配器,並且這些更改解決了問題。

相關問題