title: RecyclerView Multiple View types (View Holders) Handle item Selection count state
body: <p>I'm having a sectioned recyclerview with title header and items under that catergoty. I have two viewHolders one for the title, and other for the items that come under that category. Here is my Adapter.</p>
  
  <pre><code>public class MenuAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  
  
  public static final String TAG = MenuAdapter.class.getSimpleName();
  private Context mContext;
  private ItemClickListener itemClickListener;
  private List&lt;HeterogeneousObject&gt; heterogeneousObjects;
  
  public MenuAdapter(Context mContext, CartHelper helper) {
      this.mContext = mContext;
      heterogeneousObjects = new ArrayList&lt;&gt;();
  
  }
  
  public void itemClickListenerCallback(ItemClickListener itemClickListenerCallback) {
      this.itemClickListener = menuItemClickListenerCallback;
  }
  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
      if (viewType == 0) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_cuisine_rv, parent, false);
          return new MenuAdapterViewHolder(view);
  
      } else {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_cuisine_menu_items, parent, false);
          return new RecycledGridViewHolder(view);
      }
  
  }
  
  @Override
  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
      HeterogeneousObject object = heterogeneousObjects.get(position);
      if (object.getItemType() == 0) {
          if (object.getCuisine() != null) {
              MenuAdapterViewHolder holder = (MenuAdapterViewHolder) viewHolder;
              holder.cuisineName.setText(object.getCuisine().getCuisine_name());
  
          }
      } else {
          if (object.getMenuItem() != null) {
              RecycledGridViewHolder holder = (RecycledGridViewHolder) viewHolder;
              holder.dishName.setText(object.getMenuItem().getItemName());
              holder.dishPrice.setText(mContext.getString(R.string.rupee_symbol, object.getMenuItem().getItemPrice() * 0.01));
  
              if (object.getMenuItem().getIsVeg()) {
                  holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.veg_symbol));
                  //holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_veg_indicator));
              } else if (!object.getMenuItem().getIsVeg()) {
                  holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.non_veg_symbol));
                  //holder.vegNonVegIndicator.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_non_veg_indicator));
              }
              Single&lt;CartSelection&gt; selection = cartHelper.getCartSelectionById(Long.parseLong(object.getMenuItem().getItemId()));
              selection.subscribe(new DisposableSingleObserver&lt;CartSelection&gt;() {
                  @Override
                  public void onSuccess(CartSelection cartSelection) {
                      if (cartSelection != null) {
                          holder.incDecButton.setNumber(cartSelection.getQty(), true);
                      }
                  }
  
                  @Override
                  public void onError(Throwable e) {
  
                  }
              });
              holder.bind(object.getMenuItem(), menuItemClickListener);
          }
      }
  
  }
  
  
  @Override
  public int getItemCount() {
      return heterogeneousObjects.size();
  }
  
  @Override
  public int getItemViewType(int position) {
      super.getItemViewType(position);
      if (heterogeneousObjects.get(position).getItemType() == 0) {
          return 0;
      } else {
          return 1;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my View Holder 1</p>
  
  <pre><code>public class RecycledGridViewHolder extends RecyclerView.ViewHolder {
      @BindView(R.id.tv_si_dish_name)
      TextView dishName;
      @BindView(R.id.tv_si_dish_price)
      TextView dishPrice;
  
      @BindView(R.id.btn_id_item_dish_grid)
      IncDecButton incDecButton;
      @BindView(R.id.iv_veg_non_veg_grid)
      AppCompatImageView vegNonVegIndicator;
  
      public RecycledGridViewHolder(View itemView) {
          super(itemView);
          ButterKnife.bind(this, itemView);
      }
  
      public void bind(final MenuItem menuItem, final MenuItemClickListener menuItemClickListener) {
          incDecButton.setOnButtonsClickedListener(new IncDecButton.OnButtonsClickedListener() {
              @Override
              public void onPlusClicked(int num) {
  
                  if (menuItem.getCustomizable()) {
                      menuItemClickListener.onCustomizableItemClicked(menuItem, 1);
                      if (incDecButton.getNumber() == 0) {
                          incDecButton.setNumber(0, true);
                      }
                  } else {
                      menuItemClickListener.onPlusButtonClicked(menuItem, num);
                  }
              }
  
              @Override
              public void onMinusClicked(int num) {
                  if (menuItem.getCustomizable()) {
                      menuItemClickListener.onCustomizableItemClicked(menuItem, 0);
                  } else {
                      menuItemClickListener.onMinusButtonClicked(menuItem, num);
                  }
              }
          });
  
  }
  </code></pre>
  
  <p>View Holder 2</p>
  
  <pre><code>  class MenuAdapterViewHolder extends RecyclerView.ViewHolder {
  
      @BindView(R.id.tv_cuisine_name_menu_v2)
      TextView cuisineName;
  
  
      public MenuAdapterViewHolder(View itemView) {
          super(itemView);
          ButterKnife.bind(this, itemView);
      }
  }
  </code></pre>
  
  <p>I'm maintaining a state of item count that the list item in ViewHolder 1 has to show in  IncDecButton outside of the ViewHolder in a ROOM Db Dao, which i query as onBindViewHolder is called for each item but the views gets recycled and item count gets misplaced. </p>
  
  <p>when i only have a single view holder, i.e only View Holder 1, item counts have no issues, but it gets recycled and misplaced when there are 2 View Holders.
  I don't really seem to understand what am i doing wrong.  </p>
  
  <p>This code works fine with only a single Viewholder (Single Item Type RecyclerView).</p>