Search code examples
androidandroid-recyclerviewandroid-cardviewnestedrecyclerview

How to access all cardviews in a recyclerview, on item click of a particular cardview


I have a main recyclerview,which displays my main cardview. Now this main cardview got a child recyclerview inside,which holds the child cardview. Now when i click on the anyof the main cardview,the child recyclerview with child cardviews become visible. If i click that main cardview again,then the child recyclerview becomes invisible.

Now what's happening is, if i click a main carview,then its child recyclerview will be come visible. Now if i click another main cardview,then its child recyclerview will appear. like this

enter image description here

What i want is,if i click another main cardview,then,if any other childviews are visible , then it should become invibsle. In another words,the child rewcyclerview of the main cardview which i click should only be open. All others child recyclerviews should be invisible/ close.

Now what i want is a way to access all the main carviews in the main recyclerview,when i click a particular main cardview,so that i cam make the child recyclerview of all those main cardviews invisible.

Hope i made my point clear.

I want only one childview open at a time like this..

enter image description here

This is the onBindViewHolder of main Recyclerview holder adapter

@Override
public void onBindViewHolder(@NonNull final MainHolder holder, final int position) {

    MSeasonTanks mSeasonTanks=mSTankList.get(position);
    MoultingDataEntry moultingDataEntry=moultList.get(position);
    holder.textViewOne.setText("Tank Number : "+mSeasonTanks.getTName());

    ChildRVAdapter childRVAdapter=new ChildRVAdapter(moultingDataEntry,mSeasonTanks.getTID(),holder);

    holder.childRecyclerView.setLayoutManager(new LinearLayoutManager(context));
    holder.imageViewDirection.setImageResource(R.drawable.ic_expand);

    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(holder.childRecyclerView.getVisibility()==View.VISIBLE){
                holder.childRecyclerView.setVisibility(View.GONE);
                holder.imageViewDirection.setImageResource(R.drawable.ic_expand);
            }else{
                holder.childRecyclerView.setVisibility(View.VISIBLE);
                holder.imageViewDirection.setImageResource(R.drawable.ic_collapse);
            }
        }
    });
    holder.childRecyclerView.setAdapter(childRVAdapter);
}

So on the main carview onClick method to access all the main cardviews,so that i can get their child recyclerviews invisible if its open.

Thanks in advance..


Solution

  • What I will Suggest is.

    -> Suppose you have data for the main Recyclerview like MainRecyclerItemData, It should have one boolean flag, isExpanded

    -> isExpanded = true (Inner Recyclerview is Visible)

    -> isExpanded = false (Inner Recyclerview is hidden)

    -> Now When User click on main recycler item, Pass MainRecyclerItemData's id for the clicked item to your activity via interface to method suppose (OuterItemClicked(String id)).

    -> Now loop the wholelist, and for the wholelist make isExpanded = false except for the item with the clcked id (for it make it true).

    -> Now call adapter.notifydatasetchanged()

     //In onBindViewHolder() you should have code to handle isExpanded flag, 
    if(isExpanded){
    // Show inner recyclerview
    }else{
    // Hide inner recyclerview
    }