title: RecyclerView Viewholder reusing
body: <p>I am creating a <code>RecyclerView</code> which will expand itself when the user touches it and closes itself when the user touches it again. Below is my code:</p>
  
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.RecViewHolder&gt; {
      ArrayList&lt;String&gt; values;
      ArrayList&lt;Integer&gt; expandedPosition;
      public static class RecViewHolder extends RecyclerView.ViewHolder {
          ...
          private boolean resultsOpened = false;
          ...
          RelativeLayout favHeaderLayout;
          RelativeLayout favResultsLayout;
  
          public RecViewHolder (View itemView) {
              super(itemView);
              favHeaderLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (!resultsOpened) {
                          expandLayout();
                      } else {
                          favResultsLayout.setVisibility(View.GONE);
                          resultsOpened = false;
                      }
                  }
              });
  
          }
  
          public void expandLayout() {
              favResultsLayout.setVisibility(View.VISIBLE);
              resultsOpened = true;
              //I populate my expanded layout here
          }
      }
  
      public RecyclerViewAdapter (Context context, ArrayList&lt;String&gt; values) {
          this.context = context;
          this.values= values;
      }
  
      @Override
      public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
          RecViewHolder recViewHolder= new RecViewHolder(v);
          return recViewHolder;
      }
  
      @Override
      public void onBindViewHolder(RecViewHolder recViewHolder , int position) {
      }
  
      @Override
      public void onAttachedToRecyclerView(RecyclerView recyclerView) {
          super.onAttachedToRecyclerView(recyclerView);
      }
  
      @Override
      public int getItemCount() {
          return values.size();
      }
  
  }
  </code></pre>
  
  <p>When I touch the first view, it should expand itself. However, when the adapter has more data and I touch the first view, the view expands and another view at the bottom of the <code>RecyclerView</code> (e.g. the 8th view) expands too. How do I ensure that only the view that have been touched expand the the other stays close? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've tried storing the expanded Views position in an arraylist and in <code>onBindViewHolder</code>, check which views are expanded and expand them only:</p>
  
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.RecViewHolder&gt; {
      ArrayList&lt;String&gt; values;
      ArrayList&lt;Integer&gt; expandedPosition;
      public static class RecViewHolder extends RecyclerView.ViewHolder {
          ...
          private boolean resultsOpened = false;
          ...
          RelativeLayout favHeaderLayout;
          RelativeLayout favResultsLayout;
  
          public RecViewHolder (View itemView) {
              super(itemView);
  
  
          }
  
      }
  
      public RecyclerViewAdapter (Context context, ArrayList&lt;String&gt; values) {
          this.context = context;
          this.values= values;
      }
  
      @Override
      public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
          RecViewHolder recViewHolder= new RecViewHolder(v);
          return recViewHolder;
      }
  
      @Override
      public void onBindViewHolder(RecViewHolder recViewHolder , int position) {
          recViewHolder.favResultsLayout.setVisibility(View.GONE);
          for(int i= 0; i &lt; expandedPosition.size(); i ++){
              recViewHolder.favResultsLayout.setVisibility(View.VISIBLE);
          }
          recViewHolder.favHeaderLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                       if (!expandedPosition.contains(position)) {
                          expandLayout(recViewHolder);
                          expandedPosition.add(position);
                      } else {
                          recViewHolder.favResultsLayout.setVisibility(View.GONE);
                          for(int i = 0; i &lt; expandedPosition.size(); i ++){
                              if (expandedPosition.get(i) == position){
                                      expandedPosition.remove(i);
                              }
                          }
                      }
                  }
              });
      }
      public void expandLayout() {
              favResultsLayout.setVisibility(View.VISIBLE);
              //I populate my expanded layout here
          }
      @Override
      public void onAttachedToRecyclerView(RecyclerView recyclerView) {
          super.onAttachedToRecyclerView(recyclerView);
      }
  
      @Override
      public int getItemCount() {
          return values.size();
      }
  
  }
  </code></pre>
  
  <p>Still no luck :(</p>