title: Expandable recyclerview adapter with different layouts
body: <p>I am trying to make an <code>expandable RecyclerView adapter</code> that will have different layouts for the parent and the child view. I have never done an <code>expanable RecyclerView adapter</code> before, so I don't know if this is even possible. Here is my code:</p>
  
  <pre><code>public class GroupsAdapter extends ExpandableRecyclerViewAdapter&lt;GroupsAdapter.ParentVehicleViewHolder, GroupsAdapter.ChildItemViewHolder&gt; {
  
      //region final Fields
      private final GroupsClickListener mOnItemClickListener;
      private final Repository mRepository;
      private boolean mIsClicked;
      private int mFilterField;
      private int mFilterType;
      private int viewType;
      //endregion
  
      //region Fields
      private List&lt;ParentVehicle&gt; mGroups = new ArrayList&lt;&gt;();
      //endregion
  
      //region Constructors
      public GroupsAdapter(List&lt;ParentVehicle&gt; groups, Repository repository,
                           GroupsClickListener listener, boolean isClicked, int Layout) {
          super(groups);
          this.mOnItemClickListener = listener;
          this.mRepository = repository;
          this.mGroups = groups;
          this.mIsClicked = isClicked;
          this.viewType = Layout;
      }
      //endregion
  
  
      @Override
      public int getItemViewType(int position) {
          if (position == 0){
              viewType = 0;
          }else {
              viewType=1;
          }
          return viewType;
      }
  
      public int getViewTypeCount() {
          return 2;
      }
  
  
      public void setData(List&lt;ParentVehicle&gt; groups, boolean isClicked) {
          mGroups.clear();
          mGroups.addAll(groups);
          mIsClicked = isClicked;
          notifyDataSetChanged();
      }
  
      public void setGroup(ParentVehicle group) {
          mGroups.clear();
          mGroups.add(group);
          notifyDataSetChanged();
      }
  
      public void sortByName() {
          if (mFilterField == Constants.FIELD_NAME) {
              if (mFilterType == Constants.DECREASING) {
                  mFilterType = Constants.INCREASING;
                  Collections.sort(mGroups, new ParentVehicle.NameComparator());
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.NameComparator());
                  }
              } else {
                  mFilterType = Constants.DECREASING;
                  Collections.sort(mGroups, Collections.reverseOrder(new ParentVehicle.NameComparator()));
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), Collections.reverseOrder(new Vehicle.NameComparator()));
                  }
              }
          } else {
              mFilterType = Constants.INCREASING;
              mFilterField = Constants.FIELD_NAME;
              Collections.sort(mGroups, new ParentVehicle.NameComparator());
              if (mIsClicked) {
                  Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.NameComparator());
              }
          }
          notifyDataSetChanged();
      }
  
      public void sortByNumber() {
          if (mFilterField == Constants.FIELD_ID) {
              if (mFilterType == Constants.DECREASING) {
                  mFilterType = Constants.INCREASING;
                  Collections.sort(mGroups, new ParentVehicle.IdComparator());
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.IdComparator());
                  }
              } else {
                  mFilterType = Constants.DECREASING;
                  Collections.sort(mGroups, Collections.reverseOrder(new ParentVehicle.IdComparator()));
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), Collections.reverseOrder(new Vehicle.IdComparator()));
                  }
              }
          } else {
              mFilterType = Constants.INCREASING;
              mFilterField = Constants.FIELD_ID;
              Collections.sort(mGroups, new ParentVehicle.IdComparator());
              if (mIsClicked) {
                  Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.IdComparator());
              }
          }
          notifyDataSetChanged();
      }
  
  
      public void sortByDays() {
          if (mFilterField == Constants.FIELD_DAYS) {
              if (mFilterType == Constants.DECREASING) {
                  mFilterType = Constants.INCREASING;
                  Collections.sort(mGroups, new ParentVehicle.DaysComparator());
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.DaysComparator());
                  }
              } else {
                  mFilterType = Constants.DECREASING;
                  Collections.sort(mGroups, Collections.reverseOrder(new ParentVehicle.DaysComparator()));
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), Collections.reverseOrder(new Vehicle.DaysComparator()));
                  }
              }
          } else {
              mFilterType = Constants.INCREASING;
              mFilterField = Constants.FIELD_DAYS;
              Collections.sort(mGroups, new ParentVehicle.DaysComparator());
              if (mIsClicked) {
                  Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.DaysComparator());
              }
          }
          notifyDataSetChanged();
      }
  
      public void sortByGeo() {
          if (mFilterField == Constants.FIELD_GEO) {
              if (mFilterType == Constants.DECREASING) {
                  mFilterType = Constants.INCREASING;
                  Collections.sort(mGroups, new ParentVehicle.GeoComparator());
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.GeoComparator());
                  }
              } else {
                  mFilterType = Constants.DECREASING;
                  Collections.sort(mGroups, Collections.reverseOrder(new ParentVehicle.GeoComparator()));
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), Collections.reverseOrder(new Vehicle.GeoComparator()));
                  }
              }
          } else {
              mFilterType = Constants.INCREASING;
              mFilterField = Constants.FIELD_GEO;
              Collections.sort(mGroups, new ParentVehicle.GeoComparator());
              if (mIsClicked) {
                  Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.GeoComparator());
              }
          }
          notifyDataSetChanged();
      }
  
      public void sortByWarning() {
          if (mFilterField == Constants.FIELD_WARNING) {
              if (mFilterType == Constants.DECREASING) {
                  mFilterType = Constants.INCREASING;
                  Collections.sort(mGroups, new ParentVehicle.WarningComparator());
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.WarningComparator());
                  }
              } else {
                  mFilterType = Constants.DECREASING;
                  Collections.sort(mGroups, Collections.reverseOrder(new ParentVehicle.WarningComparator()));
  
                  if (mIsClicked) {
                      Collections.sort(mGroups.get(0).getVehicles(), Collections.reverseOrder(new Vehicle.WarningComparator()));
                  }
              }
          } else {
              mFilterType = Constants.INCREASING;
              mFilterField = Constants.FIELD_WARNING;
              Collections.sort(mGroups, new ParentVehicle.WarningComparator());
  
              if (mIsClicked) {
                  Collections.sort(mGroups.get(0).getVehicles(), new Vehicle.WarningComparator());
              }
          }
          notifyDataSetChanged();
      }
  
  
      void filter(String text) {
          List&lt;Group&gt; temp = new ArrayList();
          for (Group group : getAllGroups()) {
              if (group.getName().toLowerCase().contains(text)) {
                  temp.add(group);
              }
          }
          if (TextUtils.isEmpty(text)) {
              temp = getAllGroups();
          }
          //update recyclerview
          //setData(temp);
      }
  
      private List&lt;Group&gt; getAllGroups() {
          final List&lt;Group&gt; tempGroupList = new ArrayList&lt;&gt;();
          if (!mRepository.getCompanies().isEmpty()) {
              for (Company company : mRepository.getCompanies()) {
                  tempGroupList.addAll(Arrays.asList(company.getGroups()));
              }
          }
          return tempGroupList;
      }
  
      //region expandableAdapterMethods
      @Override
      public ParentVehicleViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
           View view;
         if (viewType==0){
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_table, parent, false);
              return new ParentVehicleViewHolder(view,viewType);
          } else if (viewType==1) {
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.delete_row, parent, false);
             return new ParentVehicleViewHolder(view,viewType);
         } return null;
      }
  
      @Override
      public ChildItemViewHolder onCreateChildViewHolder(ViewGroup child, int viewType) {
          View view;
          if (viewType == 0) {
              view = LayoutInflater.from(child.getContext()).inflate(R.layout.item_table, child, false);
              return new ChildItemViewHolder(view, viewType);
          } else if (viewType == 1) {
              view = LayoutInflater.from(child.getContext()).inflate(R.layout.delete_row, child, false);
              return new ChildItemViewHolder(view, viewType);
          }return null;
      }
  
      @Override
      public void onBindChildViewHolder(ChildItemViewHolder holder, int position, ExpandableGroup group, int childIndex) {
         int viewType = getItemViewType(position);
          if (viewType==0){
              final Vehicle vehicle = ((ParentVehicle) group).getItems().get(childIndex);
              holder.bind(vehicle, mOnItemClickListener);
          } else if (viewType==1){
              final Vehicle vehicle = ((ParentVehicle) group).getItems().get(childIndex);
              holder.bind2(vehicle, mOnItemClickListener);
          }
      }
  
      @Override
      public void onBindGroupViewHolder(ParentVehicleViewHolder holder, int position, ExpandableGroup group) {
         int viewType = getItemViewType(position);
          if (viewType==0){
              holder.bind(((ParentVehicle) group), mOnItemClickListener, mIsClicked);
          } else if (viewType==1){
              holder.bind2(((ParentVehicle) group), mIsClicked);
          }
  
      }
      //endregion
  
      static class ParentVehicleViewHolder extends GroupViewHolder {
          TextView novText;
          ImageView strelka;
          TextView day7;
          TextView day6;
          TextView day5;
          TextView day4;
          TextView day3;
          TextView day2;
          TextView day1;
          TextView mId;
          TextView mNumber;
          ImageView mBattery;
          ImageView mGsimbol;
          ImageView mWarning;
          ImageView mGroupArrow;
          private View mView;
  
          public ParentVehicleViewHolder(View itemView, int viewType) {
              super(itemView);
              this.mView = itemView;
              ButterKnife.bind(this, itemView);
              if (viewType == 0) {
                  day7 = itemView.findViewById(R.id.number_7);
                  day6 = itemView.findViewById(R.id.number_6);
                  day5=itemView.findViewById(R.id.number_5);
                  day4=itemView.findViewById(R.id.number_4);
                  day3=itemView.findViewById(R.id.number_3);
                  day2=itemView.findViewById(R.id.number_2);
                  day1=itemView.findViewById(R.id.number_1);
                  mId=itemView.findViewById(R.id.item_unit_id);
                  mNumber=itemView.findViewById(R.id.item_number);
                  mBattery=itemView.findViewById(R.id.item_battery);
                  mGsimbol=itemView.findViewById(R.id.item_gsimbol);
                  mWarning=itemView.findViewById(R.id.icon_warning);
                  mGroupArrow=itemView.findViewById(R.id.group_arrow);
  
  
              } else if (viewType==1){
                  novText=itemView.findViewById(R.id.item_numberEden);
                  strelka=itemView.findViewById(R.id.group_arrowEden);
              }
          }
  
          public void bind(ParentVehicle parentVehicle, GroupsClickListener groupsClickListener, boolean showArrow) {
              mNumber.setText(parentVehicle.getId());
              mId.setText(parentVehicle.getName());
              mId.setTextColor(mView.getContext().getResources().getColor(R.color.light_blue));
  
              mView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (parentVehicle.isCompany()) {
                          groupsClickListener.onCompanyItemClick(parentVehicle.getId());
                      } else {
                          groupsClickListener.onGroupItemClick(parentVehicle.getId());
                      }
                  }
              });
  
              mGroupArrow.setVisibility(showArrow ? View.VISIBLE : View.GONE);
  
              if (parentVehicle.getVehicles() != null &amp;&amp; !parentVehicle.getVehicles().isEmpty()) {
                  if (isGeo(parentVehicle.getVehicles())) {
                      mGsimbol.setVisibility(View.INVISIBLE);
                  } else {
                      mGsimbol.setVisibility(View.VISIBLE);
                  }
  
                  if (isWarning(parentVehicle.getVehicles())) {
                      mWarning.setVisibility(View.VISIBLE);
                  } else {
                      mWarning.setVisibility(View.INVISIBLE);
                  }
              }
              day1.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 0) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day2.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 1) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day3.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 2) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day4.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 3) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day5.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 4) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day6.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 5) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day7.setBackgroundResource(isActiveDay(parentVehicle.getVehicles(), 6) ? R.drawable.green_rectangle : R.drawable.red_rectangle);
          }
  
          private boolean isGeo(List&lt;Vehicle&gt; vehicles) {
              if (vehicles == null || vehicles.isEmpty()) {
                  return true;
              }
              for (Vehicle vehicle : vehicles) {
                  if (!vehicle.hasGeofenceAlarm()) {
                      return false;
                  }
              }
              return true;
          }
  
          private boolean isWarning(List&lt;Vehicle&gt; vehicles) {
              if (vehicles == null || vehicles.isEmpty()) {
                  return false;
              }
              for (Vehicle vehicle : vehicles) {
                  if (vehicle.isWarning()) {
                      return true;
                  }
              }
              return false;
          }
  
          private boolean isActiveDay(List&lt;Vehicle&gt; vehicles, int day) {
              if (vehicles == null || vehicles.isEmpty()) {
                  return true;
              }
              for (Vehicle vehicle : vehicles) {
                  if (vehicle.getDays() == null || vehicle.getDays().length == 0 || !vehicle.getDays()[day]) {
                      return false;
                  }
              }
              return true;
          }
  
          public void bind2(ParentVehicle parentVehicle,  boolean showArrow) {
              novText.setText(parentVehicle.getName());
              strelka.setVisibility(showArrow ? View.VISIBLE : View.GONE);
          }
      }
  
  
      static class ChildItemViewHolder extends ChildViewHolder {
  
          TextView novText2;
          TextView day7;
          TextView day6;
          TextView day5;
          TextView day4;
          TextView day3;
          TextView day2;
          TextView day1;
          TextView mId;
          ImageView mBattery;
          ImageView mGsimbol;
          ImageView mWarning;
          private View mView;
  
          public ChildItemViewHolder(View itemView, int viewType) {
              super(itemView);
              this.mView = itemView;
              ButterKnife.bind(this, itemView);
              if (viewType==0){
                  day7 = itemView.findViewById(R.id.number_7);
                  day6 = itemView.findViewById(R.id.number_6);
                  day5=itemView.findViewById(R.id.number_5);
                  day4=itemView.findViewById(R.id.number_4);
                  day3=itemView.findViewById(R.id.number_3);
                  day2=itemView.findViewById(R.id.number_2);
                  day1=itemView.findViewById(R.id.number_1);
                  mId=itemView.findViewById(R.id.item_unit_id);
                 mBattery=itemView.findViewById(R.id.item_battery);
                  mGsimbol=itemView.findViewById(R.id.item_gsimbol);
                  mWarning=itemView.findViewById(R.id.icon_warning);
              } else if (viewType==1){
                  novText2=itemView.findViewById(R.id.item_numberEden);
              }
  
  
  
          }
  
          public void bind(Vehicle vehicle, GroupsClickListener onItemClickListener) {
              mId.setText(vehicle.getName());
  
              if (vehicle.hasGeofenceAlarm()) {
                  mGsimbol.setVisibility(View.INVISIBLE);
              } else {
                  mGsimbol.setVisibility(View.VISIBLE);
              }
  
              if (vehicle.isWarning()) {
                  mWarning.setVisibility(View.VISIBLE);
              } else {
                  mWarning.setVisibility(View.INVISIBLE);
              }
  
              mView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      onItemClickListener.onGroupChildItemClick(vehicle.getId());
                  }
              });
  
              day1.setBackgroundResource(vehicle.getDays()[0] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day2.setBackgroundResource(vehicle.getDays()[1] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day3.setBackgroundResource(vehicle.getDays()[2] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day4.setBackgroundResource(vehicle.getDays()[3] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day5.setBackgroundResource(vehicle.getDays()[4] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day6.setBackgroundResource(vehicle.getDays()[5] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
              day7.setBackgroundResource(vehicle.getDays()[6] ? R.drawable.green_rectangle : R.drawable.red_rectangle);
          }
          public void bind2(Vehicle vehicle, GroupsClickListener onItemClickListener) {
              novText2.setText(vehicle.getName());}
      }
  }
  </code></pre>
  
  <p>and I am getting this error: </p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: viewType is not valid
            at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onCreateViewHolder(ExpandableRecyclerViewAdapter.java:57)
            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
            at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
            at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
            at android.view.View.measure(View.java:18930)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1109)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:649)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1129)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:649)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
            at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
            at android.view.View.measure(View.java:18930)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
            at com.android.internal.poli</p>
    
    <p>Blockquote</p>
  </blockquote>
  
  <p>What am I doing wrong?
  Why am I getting viewType is not valid?</p>