title: RecyclerView set views to be recycled
body: <p>in my app, I'm implementing a recyclerview. My dataset for this recyclerview will have varying sizes according to the options that I set for the data to be displayed on the recyclerview.</p>
  
  <p>One of the actions that I take with my recyclerview is to "expand" an item when a click is done on it, displaying further options in it. When pressing on this "expanded" item, I perform the action of "closing" it. Also, there can only be on "expanded" item at maximum at any moment.</p>
  
  <p>The thing is that I understand that recyclerview recycles its row-views when they get out of sight for improved performance. However, because I am trying to have only one "expanded" item at a time, this recycling messes it up quite a lot. </p>
  
  <p>What happens right now is that when I "expand", say the item related to position 1 of my dataset, as shown in the image below.</p>
  
  <p><a href="https://i.stack.imgur.com/VqzUt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VqzUt.png" alt="item for position 1 &quot;expanded&quot;"></a></p>
  
  <p>When I scroll down, I will see that the rowview for this item being recycled at a random chance since I will see this "expanded" view on items that I have not set to be "expanded", as shown in the image below.</p>
  
  <p><a href="https://i.stack.imgur.com/ITbCh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ITbCh.png" alt="this happened for me only scrolling down through my recyclerview"></a></p>
  
  <p>And of course, when this happens, then when I scroll back to the item that I have selected to "expand", it will be "closed" as you would have expected.</p>
  
  <p>So I have been thinking that I could resolve this problem by setting the possible number of views to be something like 80% of my dataset size will decrease the possibility of this problem occurring while still reduced, but enjoy some improved performance.</p>
  
  <p>Another solution I thought about was disabling this "expanded" view from being recycled for other views and when this "expanded" item's position comes into screen, it gets bounded to this specific view. I thought of this solution after seeing that there is a concept of "scrap" and "recycle" for recyclerview, but I am not so sure if this method is even possible because I think I have only vaguely understood this side of recyclerview.</p>
  
  <p>That being said, my question is are there ways for me to set the number of views to be recycled for a recycled view? Or even better, having one view from being recycled for items other than the "expanded" item?</p>
  
  <p>Thanks in advance.</p>
  
  <h2>EDIT:</h2>
  
  <p>here's my (I know it's very messy I'm sorry...) code for my adapter:</p>
  
  <pre><code>public class DrinkMenuItem extends RecyclerView.Adapter&lt;DrinkMenuItem.ViewHolder&gt; {
      private Context context;
      private ViewGroup parent;
      private ArrayList&lt;Drink&gt; menu;
      private ArrayList&lt;DrinkSelected&gt; selected;
      private DrinkMenuBasketItem selectedAdapter;
  
      public int expanded = -1;
      public boolean expandedVisible = false;
  
      private DrinkMenuDropdownItem dropdownAdapter;
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          public RelativeLayout layout;
          public TextView name, price;
          public ListView dropdown;
          public RelativeLayout basket;
          public boolean tabbed = false;
  
          public ViewHolder(View itemView) {
              super(itemView);
              layout = (RelativeLayout)itemView.findViewById(R.id.drink_menu_layout);
              name = (TextView)itemView.findViewById(R.id.drink_menu_name);
              price = (TextView)itemView.findViewById(R.id.drink_menu_price);
              dropdown = (ListView)itemView.findViewById(R.id.drink_menu_dropdown_list);
              basket = (RelativeLayout)itemView.findViewById(R.id.drink_menu_basket_button);
          }
      }
  
      public DrinkMenuItem(Context context, ArrayList&lt;Drink&gt; menu, ArrayList&lt;DrinkSelected&gt; selected, DrinkMenuBasketItem selectedAdapter) {
          this.context = context;
          this.menu = menu;
          this.selected = selected;
          this.selectedAdapter = selectedAdapter;
          this.dropdownAdapter = null;
      }
  
      public void updateDropdown(int requestedOption, int responsedOptionitem) {
          dropdownAdapter.updateSelectedOption(requestedOption, responsedOptionitem);
          notifyDataSetChanged();
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          this.parent = parent;
          View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_drink_menu, parent, false);
  
          return new ViewHolder(itemView);
      }
  
      // Replace the contents of a view (invoked by the layout manager)
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          Drink d = menu.get(position);
          holder.name.setText(d.getName());
          holder.price.setText(d.getPrice() + d.totalAdditionalPrice() + "원");
  
          if(position == expanded) {
              //delete dropdown
              holder.dropdown.setAdapter(null);
              menu.get(position).returnToUnselected();
              holder.price.setText(menu.get(position).getPrice() + "원");
              setListViewHeight(holder.dropdown);
  
              //reset dropdown-related stuff
              holder.tabbed = false;
              holder.basket.setVisibility(View.GONE);
          }
  
          setOnClickEvent(holder, position, parent);
      }
  
      private void setOnClickEvent(final ViewHolder holder, final int position, final ViewGroup parent) {
          holder.layout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (!holder.tabbed) {
                      //close dropdown of expanded view
                      if(expanded != -1) notifyItemChanged(expanded);
                      //make dropdown
                      dropdownAdapter = new DrinkMenuDropdownItem(context, menu, position, holder.price);
                      holder.dropdown.setAdapter(dropdownAdapter);
                      setListViewHeight(holder.dropdown);
                      //set dropdown-related stuff
                      holder.tabbed = true;
                      holder.basket.setVisibility(View.VISIBLE);
                      expanded = position;
                      expandedVisible = true;
  
                     ((RecyclerView) parent).smoothScrollToPosition(position);
                  } else {
                      //delete dropdown
                      holder.dropdown.setAdapter(null);
                      menu.get(position).returnToUnselected();
                      holder.price.setText(menu.get(position).getPrice() + "원");
                      setListViewHeight(holder.dropdown);
                      expanded = -1;
                      expandedVisible = false;
  
                      //reset dropdown-related stuff
                      holder.tabbed = false;
                      holder.basket.setVisibility(View.GONE);
                  }
              }
          });
          ...
      }
  }
  </code></pre>