Bhargavi Abhirami
Bhargavi Abhirami

Reputation: 65

add recyclerView inside another recyclerView

I have a list of data to be displayed in a recycler view and that is working fine. Now I have another dynamic list of data to be displayed inside the parent recycler-view. So I tried using another recycler-view inside the parent recycler-view, that is not working fine. It will be good if I get some idea of using recycler-view inside another one. Thanks in advance..!

I have illustrated my problem with an example:

For eg: I have a parent recyclerView with five linearLayout and I have created a child recyclerView inside the Linearlayout with visibility GONE. Now when I click the first Linearlayout I am changing the visibility of child recyclerView for the first Linearlayout to VISIBLE and attaching a separate view to it and same concept for all the other Linearlayouts. What happens is when I click first, second, third and fourth linearLayout the child recyclerView is not displaying date which I pass to it, all those first, sec, third and fourth data are accumulated and displayed in the last (i.e) inside fifth linearLayout.

Here is my parent recyclerview code:

 class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyViewHolder>
{
    RecyclerView insideCardRecyclerView,recyclerView;
    List<String> monthsWeek = new ArrayList<>();
    List<String> dealers = new ArrayList<>();
    List<String> dealersList = new ArrayList<>();
    List<String> date = new ArrayList<>();
    HashSet<String> dealersListHash = new HashSet<>();

    public CardAdapter(List<String> monthsWeek,List<String> dealers,List<String> date)
    {
        this.monthsWeek = monthsWeek;
        this.dealers = dealers;
        this.date = date;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder
    {

        ProgressBar progressBar;
        RecyclerView recyclerView;
        TextView period;
        LinearLayout linearLayoutParent,linearLayoutCardDetails;

        public MyViewHolder(View view)
        {
            super(view);
            linearLayoutParent = (LinearLayout) view.findViewById(R.id.card_view_linear_parent_layout);
            linearLayoutCardDetails = (LinearLayout) view.findViewById(R.id.linear_card_layout_details);
            period = (TextView) view.findViewById(R.id.period_summary_graph_card);
            insideCardRecyclerView = (RecyclerView) view.findViewById(R.id.summary_graph_card_view_recycler_view);
        }
    }

    @Override
    public CardAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(getActivity())
                .inflate(R.layout.summary_card_view,parent,false);
        recyclerView = (RecyclerView) parent;

        return new CardAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position)
    {
        holder.period.setText(monthsWeek.get(position));

        holder.linearLayoutParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                if(searchClick)
                {

                    for (String date1 : date)
                    {
                        if(Objects.equals(date1,monthsWeek.get(position)))
                        {
                            Log.e("Summary123 date..///", date1);
                            dealersList.add(dealers.get(date.indexOf(date1)));
                        }
                    }

                    searchClick = false;
                    holder.linearLayoutCardDetails.setVisibility(View.VISIBLE);

                    dealersListHash.addAll(dealersList);
                    dealersList.clear();
                    dealersList.addAll(dealersListHash);

//if the condition is true i am attaching another recyclerview inside this.

                        cardAdapterList = new CardAdapterList(dealersList);
                        LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,true);
                        mLayoutManager1.setReverseLayout(false);
                        insideCardRecyclerView.setLayoutManager(mLayoutManager1);
                        insideCardRecyclerView.setItemAnimator(new DefaultItemAnimator());
                        insideCardRecyclerView.setAdapter(cardAdapterList);

                }
                else
                {
                    searchClick = true;
                    holder.linearLayoutCardDetails.setVisibility(View.GONE);
                }
            }
        });
    }

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

Upvotes: 3

Views: 4648

Answers (1)

Vishal G. Gohel
Vishal G. Gohel

Reputation: 1033

I've been in trouble sometimes with RecyclerView when multiple lists are needed to be shown in a single page of the application. Its not a very good idea actually to have multiple lists in a single layout but however, the idea of having a ScrollView and the lists inside that ScrollView is even worse.

I had to implement a ListView inside a ScrollView once and yes it was not a very good experience. Firstly, my list was not scrolling at all. Then I had to add some code to disable the scrolling when the touch is detected inside the list. It was not a very good idea of solving the actual problem. I had another problem of having a fixed height of the ListView. In case of list items with dynamic heights, the solution failed.

Having two lists in the layout, one after one is not a good idea either. As the first list need to have a fixed height.

So, after searching for suggestions about how can I implement two lists in a single layout file, I found most of the developers suggests of having a single list with a header and footer if necessary. Later, I could manage to show two lists in a single RecyclerView using my custom Adapter. I thought I should save some of my code for future use and hence, you see this note.

You can refer this sample code.

Upvotes: 1

Related Questions