private void selectMenuSection(int position) {
    Section section = menuAdapter.getItem(position);
    if (section != currentSection) {
      // Switch to new section
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      Fragment f = fm.findFragmentById(R.id.content);
      if (f != null) {
        if (currentSection.shouldKeep()) {
          ft.detach(f);
        } else {
          ft.remove(f);
        }
      }
      String fragmentClassName = section.getFragmentClassName();
      if (section.shouldKeep() && ((f = fm.findFragmentByTag(fragmentClassName)) != null)) {
        ft.attach(f);
      } else {
        f = Fragment.instantiate(MainActivity.this, fragmentClassName);
        ft.add(R.id.content, f, fragmentClassName);
      }
      ft.commit();

      currentSection = section;
      updateActionBar();
      menuAdapter.notifyDataSetChanged();
    }
  }
 public int returnNewWidth() {
   int itemWidth;
   if (!scrolledEnabled || getItemCount() * getWidest() <= mainMenuAdapter.getWidth()) {
     itemWidth = mainMenuAdapter.getWidth() / getItemCount();
     Log.i("itemWidth", itemWidth + "");
   } else {
     itemWidth = getWidest();
   }
   return itemWidth;
 }
 @Override
 public int getItemCount() {
   if (mainMenuAdapter.getEditMode()) {
     return row.getNumberOfBranches() + 1;
   }
   return row.getNumberOfBranches();
 }
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    RecyclerView.LayoutParams layoutParams;
    int itemWidth;
    if (!scrolledEnabled || getItemCount() * getWidest() <= mainMenuAdapter.getWidth()) {
      itemWidth = mainMenuAdapter.getWidth() / getItemCount();
      layoutParams =
          new RecyclerView.LayoutParams(itemWidth, RecyclerView.LayoutParams.MATCH_PARENT);
      Log.i("itemWidth", itemWidth + "");
    } else {
      itemWidth = getWidest();
      layoutParams =
          new RecyclerView.LayoutParams(itemWidth, RecyclerView.LayoutParams.MATCH_PARENT);
    }
    switch (holder.getItemViewType()) {
      case CATAGORYITEM:
        CafeMenu.SubMenu subMenu = row.getBranches().get(position);
        MenuItemViewHolder menuItemViewHolder = (MenuItemViewHolder) holder;
        TextView textView = menuItemViewHolder.menuItemTextView;
        textView.setText(subMenu.getTitle());
        menuItemViewHolder.relativeLayout.setLayoutParams(layoutParams);
        break;
      case PRICEITEM:
        CafeMenu.SubMenu subMenu1 = row.getBranches().get(position);
        MenuPriceItemViewHolder menuPriceItemViewHolder = (MenuPriceItemViewHolder) holder;
        TextView textViewName = menuPriceItemViewHolder.menuPriceItemTextView;
        TextView textViewPrice = menuPriceItemViewHolder.ItemPriceTextView;
        textViewName.setText(subMenu1.getTitle());
        textViewPrice.setText(subMenu1.getPrice() + "");
        menuPriceItemViewHolder.relativeLayout.setLayoutParams(layoutParams);
        break;
      case ADDBUTTON:
        AddButtonViewHolder addButtonViewHolder = (AddButtonViewHolder) holder;
        addButtonViewHolder.relativeLayout.setLayoutParams(layoutParams);
        break;
      default:
        Log.i("DEFAULTED", "");
        MenuItemViewHolder menuItemViewHolderDef = (MenuItemViewHolder) holder;
        TextView textViewDef = menuItemViewHolderDef.menuItemTextView;
        textViewDef.setText("Defaulted");
        menuItemViewHolderDef.relativeLayout.setLayoutParams(layoutParams);
        break;
    }
  }
 public int getWidest() {
   int current = 0;
   if (row.getNumberOfBranches() == 0) {
     return current;
   }
   for (CafeMenu.SubMenu subMenu : row.getBranches()) {
     String textString = subMenu.getTitle();
     Rect bounds = new Rect();
     Paint textPaint = new Paint();
     textPaint.setTextSize(spToPx(24));
     textPaint.getTextBounds(textString, 0, textString.length(), bounds);
     if (bounds.width() > current) {
       current = bounds.width();
     }
   }
   current += (int) (24 * displayMetrics.density);
   if (current >= 2 * mainMenuAdapter.getWidth() / 3 && current <= mainMenuAdapter.getWidth()) {
     current /= 1.5;
   } else if (current >= mainMenuAdapter.getWidth()) {
     current /= 2;
   }
   return current;
 }
 // Returns the view type of the item at position for the purposes of view recycling.
 @Override
 public int getItemViewType(int position) {
   if (mainMenuAdapter.getEditMode()) {
     if (position == getItemCount() - 1) {
       return ADDBUTTON;
     } else {
       if (row.getBranches().get(position).getPrice() == null) {
         return CATAGORYITEM;
       } else {
         return PRICEITEM;
       }
     }
   } else {
     if (row.getBranches().get(position).getPrice() == null) {
       return CATAGORYITEM;
     } else {
       return PRICEITEM;
     }
   }
 }