title: Expandable ListView with custom gridlayout in it
body: <p>I watched a tutorial from this <a href="https://www.youtube.com/watch?v=wet22dHIn-0" rel="nofollow noreferrer">link</a> for creating expandable <code>recyclerview</code> without using third party application for expanding <code>recyclerview</code> and inflating custom gridlayout in it.  </p>
  
  <p>Below is the code used:  </p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  RecyclerView expanderRecyclerView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_main);
  
      expanderRecyclerView = findViewById(R.id.recyclerView);
  
      initiateExpander();
  
  }
  
  
  private void initiateExpander() {
  
      ArrayList&lt;String&gt; parentList = new ArrayList&lt;&gt;();
      ArrayList&lt;ArrayList&gt; childListHolder = new ArrayList&lt;&gt;();
  
      parentList.add("Fruits &amp; Vegetables");
      parentList.add("Beverages &amp; Health");
      parentList.add("Home &amp; Kitchen");
  
      ArrayList&lt;String&gt; childNameList = new ArrayList&lt;&gt;();
      childNameList.add("Apple");
      childNameList.add("Mango");
      childNameList.add("Banana");
  
      childListHolder.add(childNameList);
  
      childNameList = new ArrayList&lt;&gt;();
      childNameList.add("Red bull");
      childNameList.add("Maa");
      childNameList.add("Horlicks");
  
      childListHolder.add(childNameList);
  
      childNameList = new ArrayList&lt;&gt;();
      childNameList.add("Knife");
      childNameList.add("Vessels");
      childNameList.add("Spoons");
  
      childListHolder.add(childNameList);
  
      ExpandableRecyclerViewAdapter expandableCategoryRecyclerViewAdapter =
              new ExpandableRecyclerViewAdapter(getApplicationContext(), parentList,
                      childListHolder);
  
      expanderRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
  
      expanderRecyclerView.setAdapter(expandableCategoryRecyclerViewAdapter);
  }
  
  }  
  </code></pre>
  
  <p><strong>ExpandableRecyclerViewAdapter</strong></p>
  
  <pre><code>public class ExpandableRecyclerViewAdapter extends 
  RecyclerView.Adapter&lt;ExpandableRecyclerViewAdapter.ViewHolder&gt; {
  
  int previousExpandedPosition = -1, mExpandedPosition=-1;
  ArrayList&lt;String&gt; nameList = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; image = new ArrayList&lt;String&gt;();
  ArrayList&lt;Integer&gt; counter = new ArrayList&lt;Integer&gt;();
  ArrayList&lt;ArrayList&gt; itemNameList = new ArrayList&lt;ArrayList&gt;();
  boolean isExpanded=true;
  Context context;
  
  public ExpandableRecyclerViewAdapter(Context context,
                                       ArrayList&lt;String&gt; nameList,
                                       ArrayList&lt;ArrayList&gt; itemNameList) {
      this.nameList = nameList;
      this.itemNameList = itemNameList;
      this.context = context;
      Log.d("namelist", nameList.toString());
      for (int i = 0; i &lt; nameList.size(); i++) {
          counter.add(0);
      }
  
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
      TextView name;
      ImageButton dropBtn;
      RecyclerView cardRecyclerView;
      CardView cardView;
  
      public ViewHolder(View itemView) {
          super(itemView);
          name = itemView.findViewById(R.id.categoryTitle);
          dropBtn = itemView.findViewById(R.id.categoryExpandBtn);
          cardRecyclerView = itemView.findViewById(R.id.innerRecyclerView);
          cardView = itemView.findViewById(R.id.cardView);
      }
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.card_collapseview, parent, false);
      ExpandableRecyclerViewAdapter.ViewHolder vh = new ExpandableRecyclerViewAdapter.ViewHolder(v);
      return vh;
  
  }
  
  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
      holder.name.setText(nameList.get(position));
      InnerRecyclerViewAdapter itemInnerRecyclerView = new InnerRecyclerViewAdapter(context ,itemNameList.get(position));
      holder.cardRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
      holder.cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (counter.get(position) % 2 == 0) {
                  holder.cardRecyclerView.setVisibility(View.VISIBLE);
              } else {
                  holder.cardRecyclerView.setVisibility(View.GONE);
              }
              counter.set(position, counter.get(position) + 1);
          }
      });
      holder.cardRecyclerView.setAdapter(itemInnerRecyclerView);
  }
  
  @Override
  public int getItemCount() {
      return nameList.size();
  }
  }
  </code></pre>
  
  <p>InnerRecyclerViewAdapter </p>
  
  <pre><code>public class InnerRecyclerViewAdapter extends 
  RecyclerView.Adapter&lt;InnerRecyclerViewAdapter.ViewHolder&gt; {
  public ArrayList&lt;String&gt; nameList = new ArrayList&lt;String&gt;();
  Context context;
  
  public InnerRecyclerViewAdapter(Context context, ArrayList&lt;String&gt; nameList) 
  {
      this.nameList = nameList;
      this.context=context;
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
      TextView name;
      CardView cardView;
  
      public ViewHolder(View itemView) {
          super(itemView);
          name = itemView.findViewById(R.id.itemTextView);
          cardView=itemView.findViewById(R.id.cardView);
      }
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.card_expand_item_view, parent, false);
      InnerRecyclerViewAdapter.ViewHolder vh = new InnerRecyclerViewAdapter.ViewHolder(v);
      return vh;
  }
  
  @Override
  public void onBindViewHolder(ViewHolder holder, final int position) {
      holder.name.setText(nameList.get(position));
      holder.cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Toast.makeText(context, "Clicked on "+nameList.get(position),Toast.LENGTH_LONG).show();
          }
      });
  }
  
  @Override
  public int getItemCount() {
      return nameList.size();
  }
  }
  </code></pre>
  
  <p>I am unable to figure out how can I modify this code so that the <code>recyclerview</code> expands one row at a time... If any other row is already expanded, it should get closed automatically on expansion of the other row. </p>
  
  <p>Please guide me how to do that.</p>