-5

您好我要添加兩個RecyclerView相同fragment如圖as shown in image所示,但我已經做了第一個recyclinglerview click here to view implemnted。我沒有得到如何添加一個與我現有的代碼,請幫助我。我RecyclerView代碼如何添加兩個recyclerview相同fragmnets

Communication.class

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_communication_summary, container, false); 

    mRecyclerView = (RecyclerView) view.findViewById(R.id.comm_expected_recycler_view); 

    mRecyclerView.setHasFixedSize(true); 
    mRecyclerView.setHasFixedSize(true); 
    mRecyclerViewLayoutManager = new LinearLayoutManager(getContext()); 
    mRecyclerView.setLayoutManager(mRecyclerViewLayoutManager); 

private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { 

    private List<CommunicationVO> mCommunicationVOsList; 

    public MyRecyclerViewAdapter(List<CommunicationVO> communicationVOsList) { 

     mCommunicationVOsList = communicationVOsList; 
    } 

@Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.communication_summary_list_item, parent, false); 

     ViewHolder viewHolder = new ViewHolder(view); 

     return viewHolder; 
    } 
@Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 

     if (holder != null) { 

      CommunicationVO communicationVO = mCommunicationVOsList.get(position); 

      holder.expInvoiceNumber.setText(communicationVO.getExpInvoiceNumber()); 
      holder.expAmount.setText(CashPunditUtils.formatAmount(communicationVO.getExpAmount())); 
      holder.expDueDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpDueDate()), "dd-MMM-yy")); 
      holder.expPromisedDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpPromisedDate()), "dd-MMM-yy")); 

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

    public class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView expInvoiceNumber, expAmount, expDueDate, expPromisedDate; 

     public ViewHolder(View itemView) { 

      super(itemView); 

      expInvoiceNumber = (TextView) itemView.findViewById(R.id.textview_invnum); 
      expAmount = (TextView) itemView.findViewById(R.id.textview_amount); 
      expDueDate = (TextView) itemView.findViewById(R.id.textview_duedate); 
      expPromisedDate = (TextView) itemView.findViewById(R.id.textview_promisedate); 
     } 
    } 
+0

https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type –

回答

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_one" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_two" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" /> 
</LinearLayout> 

而且在Java代碼中創建相同的適配器兩個不同的實例和適配器設置爲兩個回收views.Since你有相同的看法和不同的數據,你可以使用相同的適配器爲這兩個回收視圖。希望這可以幫助你。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_communication_summary, container, false); 


    //RecyclerView One 
    mRecyclerView1 = (RecyclerView) view.findViewById(R.id.recycler_one); 
    mRecyclerView1.setHasFixedSize(true); 
    adapter1=newMyRecyclerViewAdapter(communicationVOsList); 
    mRecyclerViewLayoutManager1 = new LinearLayoutManager(getContext()); 
    mRecyclerView1.setLayoutManager(mRecyclerViewLayoutManager1); 
    mRecyclerView1.setAdapter(adapter1); 

    //RecyclerView two 
    mRecyclerView2 = (RecyclerView) view.findViewById(R.id.recycler_two); 
    adapter2=newMyRecyclerViewAdapter(communicationVOsList); 
    mRecyclerView2.setHasFixedSize(true); 
    mRecyclerViewLayoutManager2 = new LinearLayoutManager(getContext()); 
    mRecyclerView2.setLayoutManager(mRecyclerViewLayoutManager2); 
    mRecyclerView2.setAdapter(adapter2); 

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { 

     private List<CommunicationVO> mCommunicationVOsList; 
     public MyRecyclerViewAdapter(List<CommunicationVO> communicationVOsList) { 
      mCommunicationVOsList = communicationVOsList; 
     } 

     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.communication_summary_list_item, parent, false); 
      ViewHolder viewHolder = new ViewHolder(view); 
      return viewHolder; 
     } 
     @Override 
     public void onBindViewHolder(ViewHolder holder, int position) { 

      if (holder != null) { 

       CommunicationVO communicationVO = mCommunicationVOsList.get(position); 
       holder.expInvoiceNumber.setText(communicationVO.getExpInvoiceNumber()); 
       holder.expAmount.setText(CashPunditUtils.formatAmount(communicationVO.getExpAmount())); 
       holder.expDueDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpDueDate()), "dd-MMM-yy")); 
       holder.expPromisedDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpPromisedDate()), "dd-MMM-yy")); 

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

     public class ViewHolder extends RecyclerView.ViewHolder { 

      public TextView expInvoiceNumber, expAmount, expDueDate, expPromisedDate; 
      public ViewHolder(View itemView) { 
       super(itemView); 
       expInvoiceNumber = (TextView) itemView.findViewById(R.id.textview_invnum); 
       expAmount = (TextView) itemView.findViewById(R.id.textview_amount); 
       expDueDate = (TextView) itemView.findViewById(R.id.textview_duedate); 
       expPromisedDate = (TextView) itemView.findViewById(R.id.textview_promisedate); 
      } 
     } 

編輯

不要忘記在recyclerview.Add創建adapdter對象的幾行代碼,並嘗試一次。

mRecyclerViewAdapter = new MyRecyclerViewAdapter(mCommunicationVOsList); 
      mRecyclerView.setAdapter(mRecyclerViewAdapter); 
+0

人無我有在XML一個更RecyclerView已經加入,但我想怎麼填多一個RecyclerView –

+0

他是讓你免費的代碼,這真的不鼓勵在這裏。 –

+0

我不這麼認爲......但無論如何......但它有助於其他遇到類似情況的人。 – Anonymous

相關問題