2016-08-16 63 views
0

我在Android Studio的Android版本23項目中使用RecyclerViewCardViewFragmentAsyncTask。我試圖在Samsung Galaxy S7上調試應用程序。不管是什麼,我似乎嘗試codewise,我RecyclerView.Adapter實現類的onCreateViewHolderonBindViewHolder方法永遠不會被調用。RecylerView適配器onCreateViewHolder&onBindViewHolder未調用:視圖爲空

活動:

public class RecyclerViewFragmentActivity extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.todays_visit_list_container); 
     if (savedInstanceState == null) { 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      RecyclerViewFragment fragment = new RecyclerViewFragment(); 
      transaction.replace(R.id.sample_content_fragment, fragment); 
      transaction.commit(); 
     } 

    } 
} 

片段:

public class RecyclerViewFragment extends Fragment { 
    protected RecyclerView mRecyclerView; 
    protected RecyclerView.LayoutManager mLayoutManager; 
    protected RecyclerViewAdapter mAdapter; 
    protected ListRefresher aListRefresher; 

    protected class ListRefresher extends AsyncTask<Uri, Void, Void> { 
     private ArrayList<Probationer> probationers = null; 
     RecyclerViewFragment fragment = null; 

     public ListRefresher(RecyclerViewFragment fragment) { 
      this.fragment = fragment; 
     } 

     @Override 
     protected Void doInBackground(Uri... params) { 
      ArrayList<Probationer> probationers = new ArrayList<Probationer>(); 
      // getData makes a JSON call to retrieve data for RecyclerView Adapter 
      listobjects = getData(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void unused) { 
      fragment.onTaskCompleted(); 
     } 

     public ArrayList getItems() { 
      return probationers; 
     } 

    } 

    protected void onTaskCompleted() { 
      PPOTodaysVisitListAdapter adapter = new PPOTodaysVisitListAdapter(getActivity(), aListRefresher.getItems()); 
      mRecyclerView.setAdapter(adapter); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.todays_visit_list, container, false); 
     mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); 
      LinearLayoutManager llm = new LinearLayoutManager(getActivity()); 
      llm.setOrientation(LinearLayoutManager.VERTICAL); 
      llm.scrollToPosition(0); 
      mRecyclerView.setLayoutManager(llm); 
      mRecyclerView.setHasFixedSize(true); 
      aListRefresher = new ListRefresher(this); 
      aListRefresher.execute(); 
     return rootView; 
    } 
} 

適配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> { 
    private ArrayList<Probationer> probationers; 
    Context context; 

    public RecyclerViewAdapter(Context context, ArrayList<Probationer> probationers) { 
     this.probationers = probationers; 
     this.context = context; 
    } 

     public static class MyViewHolder extends RecyclerView.ViewHolder { 
      public CardView cv; 
      public TextView supervisionLevel; 
      public TextView popCodes; 
      public TextView unavailable; 
      public TextView offenderId; 
      public TextView name; 
      public TextView birthDate; 
      public TextView addressLine1; 
      public TextView addressLine2; 
      public TextView lastDrugScreen; 
      public TextView lastDrugScreenResultNeg; 
      public TextView lastDrugScreenResultPos; 
      public TextView mainCrime; 
      public TextView lastDate1Lbl; 
      public TextView lastDate2Lbl; 
      public TextView lastDate3Lbl; 
      public TextView lastDate4Lbl; 
      public TextView lastDate1; 
      public TextView nextDate1; 
      public TextView lastDate2; 
      public TextView nextDate2; 
      public TextView lastDate3; 
      public TextView nextDate3; 
      public TextView lastDate4; 
      public TextView nextDate4; 

      MyViewHolder(View itemView) { 
        super(itemView); 
        cv = (CardView) itemView.findViewById(R.id.cardView); 
      supervisionLevel = (TextView) itemView.findViewById(R.id.supervisionLevel); 
        unavailable = (TextView) itemView.findViewById(R.id.unavailable); 
        popCodes = (TextView) itemView.findViewById(R.id.popCodes); 
        . 
        . 
        . 
      } 
     } 

    @Override 
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     //Inflate the layout, initialize the View Holder 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.todays_visit_list_item, parent, false); 
     RecyclerViewAdapter.MyViewHolder holder = new RecyclerViewAdapter.MyViewHolder(v); 
     return holder; 

    } 

    @Override 
    public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder viewHolder, final int position) { 
     if (probationers != null && probationers.size() > 0) { 
      Probationer aProbationer = probationers.get(position); 
      if (aProbationer != null) { 
       viewHolder.offenderId.setText(aProbationer.getProbationerId()); 
       viewHolder.name.setText(aProbationer.getName()); 
       viewHolder.birthDate.setText(aProbationer.getDateOfBirth()); 
       viewHolder.supervisionLevel.setText(aProbationer.getSupervisionLevel()); 
       viewHolder.unavailable.setText(aProbationer.getUnavailable()); 
       . 
       . 
       . 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return probationers.size(); 
    } 
} 

RecyclerView佈局(todays_visit_list.xml):

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

CardView佈局(todays_visit_list_item.xml):

<android.support.v7.widget.CardView 
    android:id="@+id/cardView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    app:cardCornerRadius="@dimen/activity_vertical_margin" 
    app:cardElevation="@dimen/activity_vertical_margin" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <RelativeLayout 
      android:id="@+id/photolayout" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="72dip" 
      android:layout_height="100dip" 
      android:layout_marginTop="6dip"> 
      <ImageView 
       android:id="@+id/loadingphoto" 
       android:scaleType="fitEnd" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:src="@drawable/nopicture"/> 
      <ImageView 
       android:id="@+id/photo" 
       android:scaleType="fitEnd" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:visibility="gone"/> 
     </RelativeLayout> 
     <RelativeLayout 
      android:id="@+id/supmsglayout" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="72dip" 
      android:layout_height="20dip" 
      android:layout_marginTop="6dip" 
      android:layout_below="@id/photolayout" 
      android:layout_alignLeft="@id/photolayout"> 
      <TextView android:id="@+id/supervisionLevel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="6dip" 
       android:layout_marginLeft="2dip" 
       android:textSize="11sp" 
       android:textStyle="bold"/> 
      <TextView android:id="@+id/unavailable" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:textStyle="bold" 
       android:layout_toRightOf="@id/supervisionLevel" 
       android:layout_alignTop="@id/supervisionLevel"/> 
      <TextView android:id="@+id/popCodes" 
       android:layout_width="76dip" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dip" 
       android:textSize="11sp" 
       android:textColor="#E79A00" 
       android:textStyle="bold" 
       android:layout_below="@id/supervisionLevel" 
       android:layout_alignLeft="@id/supervisionLevel"/> 
     </RelativeLayout> 
     <RelativeLayout 
      android:id="@+id/contentlayout" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="300dip" 
      android:layout_height="100dip" 
      android:layout_marginTop="8dip" 
      android:layout_marginLeft="4dip" 
      android:layout_toRightOf="@id/photolayout" 
      android:layout_alignTop="@id/photolayout"> 
      <TextView android:id="@+id/offenderIdLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Offender ID: " 
       android:textStyle="bold" 
       android:textSize="11sp"/> 
      <TextView android:id="@+id/offenderId" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/offenderIdLbl" 
       android:layout_alignTop="@id/offenderIdLbl"/> 
      <TextView android:id="@+id/nameLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Name: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/offenderIdLbl" 
       android:layout_alignLeft="@id/offenderIdLbl"/> 
      <TextView android:id="@+id/name" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/nameLbl" 
       android:layout_alignTop="@id/nameLbl"/> 
      <TextView android:id="@+id/birthDateLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Birth Date: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/nameLbl" 
       android:layout_alignLeft="@id/nameLbl"/> 
      <TextView android:id="@+id/birthDate" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/birthDateLbl" 
       android:layout_alignTop="@id/birthDateLbl"/> 
      <TextView android:id="@+id/addrLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Address: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/birthDateLbl" 
       android:layout_alignLeft="@id/birthDateLbl"/> 
      <TextView 
       android:id="@+id/addressLine1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/addrLbl" 
       android:layout_alignTop="@id/addrLbl"/> 
      <TextView 
       android:id="@+id/addressLine2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/addressLine1" 
       android:layout_alignLeft="@id/addressLine1"/> 
      <TextView android:id="@+id/lastDrugScreenLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Drug Scr: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/addressLine2" 
       android:layout_alignLeft="@id/addrLbl"/> 
      <TextView android:id="@+id/lastDrugScreen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/lastDrugScreenLbl" 
       android:layout_alignTop="@id/lastDrugScreenLbl"/> 
      <TextView android:id="@+id/lastDrugScreenResultNeg" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/lastDrugScreen" 
       android:layout_alignTop="@id/lastDrugScreen"/> 
      <TextView android:id="@+id/lastDrugScreenResultPos" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:textColor="#d7144b" 
       android:textStyle="bold" 
       android:layout_toRightOf="@id/lastDrugScreen" 
       android:layout_alignTop="@id/lastDrugScreen"/> 
      <TextView android:id="@+id/mainCrimeLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Crime: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDrugScreenLbl" 
       android:layout_alignLeft="@id/lastDrugScreenLbl"/> 
      <TextView android:id="@+id/mainCrime" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_toRightOf="@id/mainCrimeLbl" 
       android:layout_alignTop="@id/mainCrimeLbl"/> 
      <TextView android:id="@+id/lastDate1Lbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="33dip" 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/mainCrimeLbl" 
       android:layout_alignLeft="@id/mainCrimeLbl"/> 
      <TextView android:id="@+id/lastDate2Lbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="34dip" 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/mainCrimeLbl" 
       android:layout_toRightOf="@id/lastDate1Lbl"/> 
      <TextView android:id="@+id/lastDate3Lbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="86dip" 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/mainCrimeLbl" 
       android:layout_toRightOf="@id/lastDate1Lbl"/> 
      <TextView android:id="@+id/lastDate4Lbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="140dip" 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/mainCrimeLbl" 
       android:layout_toRightOf="@id/lastDate1Lbl"/> 
      <TextView android:id="@+id/lastLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Last: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate1Lbl" 
       android:layout_alignLeft="@id/mainCrimeLbl"/> 
      <TextView android:id="@+id/nextLbl" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Next: " 
       android:textStyle="bold" 
       android:textSize="11sp" 
       android:layout_below="@id/lastLbl" 
       android:layout_alignLeft="@id/lastLbl"/> 
      <TextView android:id="@+id/lastDate1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate1Lbl" 
       android:layout_alignLeft="@id/lastDate1Lbl"/> 
      <TextView android:id="@+id/nextDate1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate1" 
       android:layout_alignLeft="@id/lastDate1"/> 
      <TextView android:id="@+id/lastDate2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate2Lbl" 
       android:layout_alignLeft="@id/lastDate2Lbl"/> 
      <TextView android:id="@+id/nextDate2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate2" 
       android:layout_alignLeft="@id/lastDate2"/> 
      <TextView android:id="@+id/lastDate3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate3Lbl" 
       android:layout_alignLeft="@id/lastDate3Lbl"/> 
      <TextView android:id="@+id/nextDate3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate3" 
       android:layout_alignLeft="@id/lastDate3"/> 
      <TextView android:id="@+id/lastDate4" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate4Lbl" 
       android:layout_alignLeft="@id/lastDate4Lbl"/> 
      <TextView android:id="@+id/nextDate4" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="11sp" 
       android:layout_below="@id/lastDate4" 
       android:layout_alignLeft="@id/lastDate4"/> 
     </RelativeLayout> 
     <RelativeLayout 
     android:id="@+id/undolayout" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="40dip" 
     android:layout_height="100dip" 
     android:layout_marginTop="8dip" 
     android:layout_marginLeft="4dip" 
     android:layout_toRightOf="@id/contentlayout" 
     android:layout_alignTop="@id/contentlayout"> 
      <Button 
       android:id="@+id/undo_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/button_undo" 
       android:textAllCaps="true" 
       android:textColor="@android:color/white" 
       android:layout_gravity="end|center_vertical" 
       style="@style/Base.Widget.AppCompat.Button.Borderless" 
       /> 
     </RelativeLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

我的JSON呼叫被返回數據,並且AsyncTask正在呼叫回Fragment和執行方法onTaskCompleted()。當我創建的RecyclerViewAdapter的情況下,ArrayList包含的項目。唯一的問題是RecyclerView從不顯示任何東西。的RecyclerViewAdapteronCreateViewHolderonBindViewHolder方法永遠不會被調用。

我已經玩了兩種佈局的所有元素的layout_widthlayout_height,認爲這是造成問題的原因。我還特意將其實例化後,並在RecyclerView設置之前,呼籲RecyclerViewAdapternotifyDataSetChanged()。那裏也沒有喜樂。

沒有人在這裏看不到任何明顯的問題,可能會導致該RecyclerView不渲染?

我也好奇,究竟是什麼觸發了適配器上這兩種方法的調用,如果我明白這一點,也許我可以判斷爲什麼沒有被觸發事件。

+0

你創建一個自定義適配器? – Chisko

+0

是的我有一個自定義適配器。它是名爲RecyclerViewAdapter的類 – dpspae03

回答

0

嘗試將適配器連接到RecyclerView早期的生命週期(的onCreate是一個不錯的選擇),而當你的AsyncTask做只能通過notifyDataSetChanged()

通知適配器此外,請確保您設置一個LayoutManager,這樣:

recycler.setLayoutManager(new LinearLayoutManager(this));

由於您RecyclerView生活片段裏,你的情況將是:

recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

關於你的問題

我也好奇,究竟是什麼觸發的適配器上這兩個 方法的調用......如果我能明白這一點,也許我可以 確定爲什麼事件沒有被觸發。

當呈現列表的時間到達時,它必須主要處理您的數據集大小。在你的情況(和一次我的),缺少設置LayoutManager,不知何故導致不呈現。

您已經有了一個對上下文的引用在您的適配器,您可以嘗試做:

@Override 
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(context).inflate(R.layout.todays_visit_list_item, parent, false); // 
     RecyclerViewAdapter.MyViewHolder holder = new RecyclerViewAdapter.MyViewHolder(v); 
     return holder;  
    } 
+0

感謝您的建議。我嘗試使用一個空的ArrayList將適配器設置到我的Fragment的onCreateView方法中的RecyclerView中,然後在我的AsyncTask完成後通知適配器該數據集已更改,但仍未呈現任何內容。我確實注意到,以下警告/錯誤在控制檯中:W/DisplayListCanvas:DisplayListCanvas在未綁定的RenderNode上啓動(不含mOwningView) E/ViewRootImpl:sendUserActionEvent()mView == null這是否是一條線索? – dpspae03

+0

您是否也設置了LayoutManager? – Chisko

+0

是的,我已經在做LinearLayoutManager llm = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(llm); – dpspae03

相關問題