title: Expand/collapse a nested RecyclerView
body: <p>I have a RecyclerView adapter with multiple ViewHolders. Each ViewHolder has a header TextView and a nested RecyclerView which was working fine. But I wanted to implement an expand/collapse function so that nested RecyclerView is hidden until the header is clicked. I used this method <a href="https://stackoverflow.com/questions/27203817/recyclerview-expand-collapse-items">RecyclerView expand/collapse items</a>. It works but when I click the header to expand the nested recylerview, the recyclerview doesn't populate any data. To be clear, it retrieves data but it's not visible. Any ideas why this might be? </p>
  
  <p>This is my onBindViewMethod:</p>
  
  <pre><code>public class EligibilityAdapter extends RecyclerView.Adapter&lt;EligibilityAdapter.ViewHolder&gt; {
  
  private Context mContext;
  private List&lt;EligibilityDetails&gt; mEligsList;
  private List&lt;Items&gt; mItemslist;
  private LayoutInflater inflater;
  private int mExpandedPosition = -1;
  
  public EligibilityAdapter(Context context, List&lt;EligibilityDetails&gt; eligsList) {
      mContext = context;
      mEligsList = eligsList;
      inflater = LayoutInflater.from(context);
  }
  
  @Override
  public int getItemViewType(int position) {
      switch (position) {
          case 0:
              return R.layout.rv_eligs_item_domestic;
          case 1:
              return R.layout.rv_eligs_item_overseas;
          default:
              return R.layout.rv_eligs_item_military;
      }
  }
  
  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      inflater = LayoutInflater.from(viewGroup.getContext());
      View view = inflater.inflate(i, viewGroup, false);
      ViewHolder holder = null;
      switch (i) {
          case R.layout.rv_eligs_item_domestic:
              holder = new DomesticViewHolder(view);
              break;
          case R.layout.rv_eligs_item_overseas:
              holder = new OverseasViewHolder(view);
              break;
          case R.layout.rv_eligs_item_military:
              holder = new MilitaryViewHolder(view);
              break;
      }
      return holder;
  }
  
  @Override
  public int getItemCount() {
      return mEligsList.size();
  }
  
  public abstract class ViewHolder extends RecyclerView.ViewHolder {
      RecyclerView itemsRv;
      TextView mHeader;
      ItemsAdapter adapter;
  
      public ViewHolder(View itemView) {
          super(itemView);
          mHeader = (TextView) itemView.findViewById(R.id.header_tv);
          itemsRv = itemView.findViewById(R.id.recyclerViewItems);
      }
      public void setData(List&lt;Items&gt; list) {
          adapter.updateList(list);
      }
      abstract void bind(EligibilityDetails item);
  }
  
  public class DomesticViewHolder extends ViewHolder {
  
      TextView mHeader;
      RecyclerView itemsRv;
      ItemsAdapter adapter;
  
      public DomesticViewHolder(View itemView) {
          super(itemView);
          mHeader = (TextView) itemView.findViewById(R.id.header_tv);
          itemsRv = itemView.findViewById(R.id.recyclerViewItems);
      }
      public void setData(List&lt;Items&gt; list) {
          adapter.updateList(list);
      }
      @Override
      void bind(EligibilityDetails eligibilityDetails) {
          mHeader.setText(eligibilityDetails.getRequirementHeader());
          mItemslist = eligibilityDetails.getItemsList();
          ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
          itemsRv.setHasFixedSize(true);
          itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
          itemsRv.setAdapter(itemsAdapter);
          itemsRv.setNestedScrollingEnabled(false);
      }
  }
  
  public class OverseasViewHolder extends ViewHolder {
  
      TextView mHeader;
      RecyclerView itemsRv;
      ItemsAdapter adapter;
  
      public OverseasViewHolder(View itemView) {
          super(itemView);
          mHeader = (TextView) itemView.findViewById(R.id.header_tv);
          itemsRv = itemView.findViewById(R.id.recyclerViewItems);
      }
      public void setData(List&lt;Items&gt; list) {
          adapter.updateList(list);
      }
  
      @Override
      void bind(EligibilityDetails eligibilityDetails) {
          mHeader.setText(eligibilityDetails.getRequirementHeader());
          mItemslist = eligibilityDetails.getItemsList();
          ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
          itemsRv.setHasFixedSize(true);
          itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
          itemsRv.setAdapter(itemsAdapter);
          itemsRv.setNestedScrollingEnabled(false);
      }
  }
  
  public class MilitaryViewHolder extends ViewHolder {
  
      TextView mHeader;
      RecyclerView itemsRv;
      ItemsAdapter adapter;
  
      public MilitaryViewHolder(View itemView) {
          super(itemView);
          mHeader = (TextView) itemView.findViewById(R.id.header_tv);
          itemsRv = itemView.findViewById(R.id.recyclerViewItems);
      }
      public void setData(List&lt;Items&gt; list) {
          adapter.updateList(list);
      }
  
      @Override
      void bind(EligibilityDetails eligibilityDetails) {
          mHeader.setText(eligibilityDetails.getRequirementHeader());
          mItemslist = eligibilityDetails.getItemsList();
          final ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
          itemsRv.setHasFixedSize(true);
          itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
          itemsRv.setAdapter(itemsAdapter);
          itemsRv.setNestedScrollingEnabled(false);
      }
  }
  
  @Override
  public void onBindViewHolder(@NonNull final EligibilityAdapter.ViewHolder viewHolder, int i) {
      viewHolder.bind(mEligsList.get(i));
  
      final boolean isExpanded = i == mExpandedPosition;
      viewHolder.itemsRv.setVisibility(isExpanded?View.VISIBLE:View.GONE);
      viewHolder.itemView.setActivated(isExpanded);
      viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              mExpandedPosition = isExpanded ? -1:viewHolder.getAdapterPosition();
              ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
              viewHolder.itemsRv.setAdapter(itemsAdapter);
              //TransitionManager.beginDelayedTransition(recyclerView);
              notifyDataSetChanged();
          }
      });
  }
  </code></pre>
  
  <p>where itemsRv is the nested RecyclerView.
  I've tried moving this logic to the individual viewholders and moving the recyclerview logic here like setting the adapter inside of the onClick method. Each time it comes up blank.</p>
  
  <p>Thanks in advance.</p>