title: Collapsing all other Recyclerview items when one is expanded
body: <p>I have a well-setup expandable recyclerview. When clicked on any item, it gets expanded to show more details. But I want a change here. When one item is clicked to expand, all other items should be collapsed. Here's my adapter code:</p>
  
  <pre><code>public class DataAdapter extends RecyclerView.Adapter&lt;DataAdapter.ViewHolder&gt; {
      private ArrayList&lt;ListItem&gt; android_versions;
      private List&lt;ListItem&gt; listItems;
      private LayoutInflater layoutInflater;
      private Animation animationUp, animationDown;
      private Context context;
      private final int COUNTDOWN_RUNNING_TIME = 500;
      int i =0;
  
      public DataAdapter(Context context, java.util.List&lt;ListItem&gt; android_versions,
                         Animation animationUp, Animation animationDown) {
          this.context = context;
          this.listItems = android_versions;
          this.animationDown = animationDown;
          this.animationUp = animationUp;
      }
  
      @Override
      public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
          return new ViewHolder(view);
      }
  
      public String getRandomColor(int a){
          String colors[] = {"#ef5350","#f44336","#ef5350"};
          return colors[a];
      }
  
      @Override
      public void onBindViewHolder(final ViewHolder holder, int i) {
          ListItem listItem = listItems.get(i);
          if (i%2==2)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
          else if (i%2==1)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
          else if (i%2==0)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
  
          holder.tv_android.setText(listItem.getSubName());
          holder.contentLayout.setText(listItem.getContent());
          holder.expandit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  if (holder.contentLayout.isShown()) {
                      holder.contentLayout.startAnimation(animationUp);
                      CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                          @Override
                          public void onTick(long millisUntilFinished) {
                          }
  
                          @Override
                          public void onFinish() {
                              holder.contentLayout.setVisibility(View.GONE);
                          }
                      };
                      countDownTimerStatic.start();
                  } else {
                      holder.contentLayout.setVisibility(View.VISIBLE);
                      holder.contentLayout.startAnimation(animationDown);
                  }
              }
          });
      }
      @Override
      public int getItemCount() {
          return listItems.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder{
          TextView tv_android;
          private TextView contentLayout;
          RelativeLayout expandit;
          public ViewHolder(View view) {
              super(view);
              tv_android = (TextView)view.findViewById(R.id.tv_android);
              contentLayout = (TextView) view.findViewById(R.id.content);
              expandit = (RelativeLayout)view.findViewById(R.id.expandit);
          }
      }
  }
  </code></pre>
  
  <p>I think it's not possible to change viewHolder's content by position. So how can I hide all other items' content by position. I don't think other code is required so I haven't included it.</p>
  
  <p>Edit: Code after Ashish's suggestion:</p>
  
  <pre><code>  private void changeStateOfItemsInLayout(ListItem listItem) {
          for (int i = 0; i &lt; listItems.size(); i++) {
              if (listItems.get(i) == listItem)
                  listItem.setShouldBeExpanded(true);
              else
                  listItem.setShouldBeExpanded(false);
          }
      }
  
      @Override
      public void onBindViewHolder(final ViewHolder holder, int i) {
          ListItem listItem = listItems.get(i);
          if (i%2==2)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(0)));
          else if (i%2==1)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(1)));
          else if (i%2==0)
              holder.itemView.setBackgroundColor(Color.parseColor(getRandomColor(2)));
  
          holder.tv_android.setText(listItem.getSubName());
          holder.contentLayout.setText(listItem.getContent());
          if(listItem.getShouldBeExpanded()){
              holder.contentLayout.setVisibility(View.VISIBLE);
              holder.contentLayout.startAnimation(animationDown);
          }else{
              holder.contentLayout.startAnimation(animationUp);
              CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                  @Override
                  public void onTick(long millisUntilFinished) {
                  }
  
                  @Override
                  public void onFinish() {
                      holder.contentLayout.setVisibility(View.GONE);
                  }
              };
              countDownTimerStatic.start();
          }
  
          holder.expandit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (holder.contentLayout.isShown()) {
                      holder.contentLayout.startAnimation(animationUp);
                      CountDownTimer countDownTimerStatic = new CountDownTimer(COUNTDOWN_RUNNING_TIME, 16) {
                          @Override
                          public void onTick(long millisUntilFinished) {
                          }
  
                          @Override
                          public void onFinish() {
                              holder.contentLayout.setVisibility(View.GONE);
                          }
                      };
                      countDownTimerStatic.start();
                  } else {
                      holder.contentLayout.setVisibility(View.VISIBLE);
                      holder.contentLayout.startAnimation(animationDown);
                  }
                  listItems.get(holder.getLayoutPosition()).setShouldBeExpanded(true);
                  changeStateOfItemsInLayout(listItems.get(holder.getLayoutPosition()));
                  notifyDataSetChanged();
              }
          });
      }
  </code></pre>
  
  <p>Although in the onClick, I still have the code to expand/collapse layout, even if I remove it, it doesn't work.</p>
  
  <p>Here's my modal class if required:</p>
  
  <pre><code>public class ListItem {
  
      private String subName;
      private String content;
      private String param;
      private Boolean shouldBeExpanded=false;
  
      public Boolean getShouldBeExpanded() {
          return shouldBeExpanded;
      }
  
      public void setShouldBeExpanded(Boolean shouldBeExpanded) {
          this.shouldBeExpanded = shouldBeExpanded;
      }
  
      public ListItem(String subName, String content) {
          this.subName = subName;
          this.content = content;
          this.param = "Nothing";
      }
  
      public String getParam() {
          return param;
      }
  
      public ListItem(String subName, String content, String param) {
  
          this.subName = subName;
          this.content = content;
          this.param = param;
      }
  
      public String getSubName() {
          return subName;
      }
  
      public String getContent() {
          return content;
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> As requested, here's the code for animationUp file and animationDown file:</p>
  
  <p>animationUp.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;set xmlns:android="http://schemas.android.com/apk/res/android"
      android:fillAfter="true" &gt;
  
      &lt;scale
          android:duration="200"
          android:fromXScale="1.0"
          android:fromYScale="1.0"
          android:interpolator="@android:anim/linear_interpolator"
          android:toXScale="1.0"
          android:toYScale="0.0" /&gt;
  
  &lt;/set&gt;
  </code></pre>
  
  <p>animationDown.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;set xmlns:android="http://schemas.android.com/apk/res/android"
      android:fillAfter="true"&gt;
  
      &lt;scale
          android:duration="200"
          android:fromXScale="1.0"
          android:fromYScale="0.0"
          android:interpolator="@android:anim/accelerate_interpolator"
          android:toXScale="1.0"
          android:toYScale="1.0" /&gt;
  
  &lt;/set&gt;
  </code></pre>