title: Height of recycler view item changes unexpectedly when clicked
body: <p>I have a fragment with a search view and a recycler view with which you can select one item at a time (the selected item will be highlighted). Item height may vary depending on the content being displayed. When a recycler views item is clicked, I am changing the background color of that item, and then storing the selected item to a variable for later use. However, when the item is being selected, height of it, is also changing dramatically. Its like item's detail is being collapsed or expanded unintentionally (just seems not really).</p>
  
  <p>I am not very sure, but I think it has something to do with the reuse of cell. I am unable to figure out the exact issue as I just started working on android.</p>
  
  <p>Here is the <a href="https://www.dropbox.com/s/f58lgn6inni6mf1/Recycler%20View%20Issue.mov?dl=0" rel="nofollow noreferrer">link to the video</a> showing the problem, in case needed.</p>
  
  <p>How can I fix this?</p>
  
  <p>My adapter class is-</p>
  
  <pre><code>public class CodeListAdapter extends RecyclerView.Adapter&lt;CodeListAdapter.ViewHolder&gt; {
  
      private List&lt;Code&gt; codes;
      private Code selectedCode;
      private int selectedPosition = -1;
  
      public CodeListAdapter(Code selectedCode) {
          this.selectedCode = selectedCode;
      }
  
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_code, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          Code code = codes.get(position);
          holder.codeIdentifier.setText(code.getCodeIdentifier());
  
          if (code.getCodeType() == null || code.getCodeType().isEmpty()) {
              holder.codeType.setVisibility(View.GONE);
          } else {
              holder.codeType.setText(code.getCodeType());
          }
  
          if (code.getCodeDescription() == null || code.getCodeDescription().isEmpty()) {
              holder.codeDescription.setVisibility(View.GONE);
          } else {
              holder.codeDescription.setText(code.getCodeDescription());
          }
  
          if (selectedCode == code) {
              holder.itemLayout.setBackgroundResource(R.color.colorEmphasis);
          } else {
              holder.itemLayout.setBackgroundResource(R.color.colorWhite);
          }
  
          final ViewHolder viewHolder = holder;
          holder.itemLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View itemView) {
                  int currentPosition = viewHolder.getAdapterPosition();
                  if (currentPosition != selectedPosition) {
                      selectedCode = codes.get(currentPosition);
                      notifyItemChanged(selectedPosition);
                      selectedPosition = currentPosition;
                      notifyItemChanged(selectedPosition);
                  }
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return codes.size();
      }
  
      public void setCodes(List&lt;Code&gt; codes) {
          this.codes = codes;
      }
  
      public Code getCode() {
          return selectedCode;
      }
  
      static class ViewHolder extends RecyclerView.ViewHolder {
          View itemLayout;
          TextView codeIdentifier;
          TextView codeType;
          TextView codeDescription;
  
          ViewHolder(View view) {
              super(view);
              itemLayout = view.findViewById(R.id.item_layout);
              codeIdentifier = (TextView) view.findViewById(R.id.codeIdentifier);
              codeType = (TextView) view.findViewById(R.id.codeType);
              codeDescription = (TextView) view.findViewById(R.id.codeDescription);
          }
      }
  }
  </code></pre>
  
  <p>Item's layout file is-</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/black75PercentColor"
      android:paddingBottom="1dp"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/item_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/white"
          android:paddingBottom="6dp"
          android:paddingEnd="12dp"
          android:paddingStart="12dp"
          android:paddingTop="6dp"&gt;
  
          &lt;TextView
              android:id="@+id/codeIdentifier"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentStart="true"
              android:layout_alignParentTop="true" /&gt;
  
          &lt;TextView
              android:id="@+id/codeType"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/codeIdentifier" /&gt;
  
          &lt;TextView
              android:id="@+id/codeDescription"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@id/codeType" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>Layout file is-</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
  
      &lt;SearchView
          android:id="@+id/searchView"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:queryHint="@string/key_search_code" /&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:background="@color/colorPrimary"
          android:paddingEnd="12dp"
          android:paddingStart="12dp"&gt;
  
          &lt;TextView
              android:id="@+id/headerTextView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_vertical"
              android:text="Codes"
              android:textColor="@color/white"
              android:textSize="20sp" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;mdbilling.ca.mdbilling_android.Customs.ExtendedRecyclerView
          android:id="@+id/diagnosticCodeRecyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;RelativeLayout
          android:id="@android:id/empty"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center"
          android:gravity="center"
          android:paddingEnd="12dp"
          android:paddingStart="12dp"&gt;
  
          &lt;TextView
              android:id="@+id/messageHeader"
              style="?android:attr/textAppearanceLarge"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textAlignment="center" /&gt;
  
          &lt;TextView
              android:id="@+id/messageBody"
              style="?android:attr/textAppearanceMedium"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/messageHeader"
              android:layout_marginTop="6dp"
              android:textAlignment="center" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Customs.ExtendedRecyclerView</strong> here is nothing but an extension (<a href="https://gist.github.com/adelnizamutdinov/31c8f054d1af4588dc5c" rel="nofollow noreferrer">from this gist</a>) of recycler view which have the capability of displaying <code>@android:id/empty</code> view, whenever dataset in adapter is empty.</p>
  
  <p>Did I do anything wrong or is it the way it is?</p>