[Solved] How add two recyclerview same fragmnets


<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>

And in your Java code create two different instances of the same adapter and set that adapter to both recycler views.Since you have same view and different data you can use same adapter for both recycler view.Hope this helps you.

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 = new MyRecyclerViewAdapter(communicationVOsList);
    mRecyclerViewLayoutManager1 = new LinearLayoutManager(getContext());
    mRecyclerView1.setLayoutManager(mRecyclerViewLayoutManager1);
    mRecyclerView1.setAdapter(adapter1);

    //RecyclerView  two
    mRecyclerView2 = (RecyclerView) view.findViewById(R.id.recycler_two);
    adapter2 = new MyRecyclerViewAdapter(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);
            }
        }

EDIT:

Don’t forget to create adapter object in recyclerview. Add these lines of code and try once.

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

13

solved How add two recyclerview same fragmnets