具有不同布局的可扩展recyclerview适配器

时间:2018-07-03 15:59:24

标签: android recycler-adapter recyclerview-layout

我正在尝试制作一个expandable RecyclerView adapter,以使其父视图和子视图具有不同的布局。我以前从未做过expanable RecyclerView adapter,所以我不知道这是否有可能。这是我的代码:

public class GroupsAdapter extends ExpandableRecyclerViewAdapter<GroupsAdapter.ParentVehicleViewHolder, GroupsAdapter.ChildItemViewHolder> {

    //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<ParentVehicle> mGroups = new ArrayList<>();
    //endregion

    //region Constructors
    public GroupsAdapter(List<ParentVehicle> 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<ParentVehicle> 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<Group> 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<Group> getAllGroups() {
        final List<Group> tempGroupList = new ArrayList<>();
        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 && !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<Vehicle> vehicles) {
            if (vehicles == null || vehicles.isEmpty()) {
                return true;
            }
            for (Vehicle vehicle : vehicles) {
                if (!vehicle.hasGeofenceAlarm()) {
                    return false;
                }
            }
            return true;
        }

        private boolean isWarning(List<Vehicle> vehicles) {
            if (vehicles == null || vehicles.isEmpty()) {
                return false;
            }
            for (Vehicle vehicle : vehicles) {
                if (vehicle.isWarning()) {
                    return true;
                }
            }
            return false;
        }

        private boolean isActiveDay(List<Vehicle> 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());}
    }
}

我收到此错误:

  

java.lang.IllegalArgumentException:viewType无效           在com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onCreateViewHolder(ExpandableRecyclerViewAdapter.java:57)           在android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6493)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5563)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5559)           在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2229)           在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)           在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)           在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)           在android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:765)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:647)           在android.view.View.measure(View.java:18930)           在android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:1109)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:649)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:765)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:647)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:1129)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:649)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:765)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:647)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)           在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:765)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:647)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:765)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:647)           在android.view.View.measure(View.java:18930)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)           在com.android.internal.poli

     

Blockquote

我在做什么错? 为什么我的viewType无效?

1 个答案:

答案 0 :(得分:0)

我尝试使用相同的ExpandableRecyclerViewAdapter,但我想指出几点。

首先要考虑到您的错误,这是因为如果您查看ExpandableRecyclerViewAdapter类的onCreateViewHolder方法,则期望两者之一-ExpandableListPosition.GROUP或ExpandableListPosition.CHILD分别具有值2和1。

现在,您犯的第一个错误是,您将viewType值的getItemViewType覆盖为0和1,其中在库中已经使用了in 1,因此根据库类,它会向您抛出非法异常,这是正确的。

现在,为了像我一样获得多个子视图,

首先使用MultiTypeExpandableRecyclerViewAdapter扩展适配器类 并创建2个可扩展ChildViewHolder的clase,然后,如果您看到类扩展声明,它现在将如下所示:

public class ScoreCardDataAdapter extends MultiTypeExpandableRecyclerViewAdapter<TeamScoreCardViewHolder, ChildViewHolder> {

此外,我为viewtype使用了两个变量,分别为TYPE_HEADER和TYPE_ITEM,其值分别为3和4。您可以使用任何变量,但要确保它大于2。

,然后覆盖以下方法:

  @Override
  public boolean isChild(int viewType) {
    return viewType == TYPE_HEADER || viewType == TYPE_ITEM;
  }

在此方法中,我实现了一个方法,说第一个位置是标题,并针对该情况返回TYPE_HEADER。

 @Override
        public int getChildViewType(int position, ExpandableGroup group, int childIndex) {
            if(isPositionHeader(childIndex))
                return TYPE_HEADER;
            else
                return TYPE_ITEM;
        }

private boolean isPositionHeader(int position) {
    return position == 0;
}

然后在您的onCreateChildViewHolder中

您可以检查视图类型并返回期望的VIEWHOLDER。

并通过调用以下方法来绑定视图持有者:

  @Override
  public void onBindChildViewHolder(ChildViewHolder holder, int flatPosition, ExpandableGroup group,
      int childIndex) {
    int viewType = getItemViewType(flatPosition);
//here based on the view type pass the appropriate data.
}

请参见下面我完整的适配器类实现,以供您参考。

public class ScoreCardDataAdapter extends MultiTypeExpandableRecyclerViewAdapter<TeamScoreCardViewHolder, ChildViewHolder> {

private static final int TYPE_HEADER = 3;
private static final int TYPE_ITEM = 4;

public ScoreCardDataAdapter(Fragment fragment, List<? extends ExpandableGroup> groups) {
    super(groups);
}

@Override
public TeamScoreCardViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_score_card_parent_view_holder, parent, false);
    return new TeamScoreCardViewHolder(view);
}

@Override
public ChildViewHolder onCreateChildViewHolder(ViewGroup parent, final int viewType) {
    if(viewType == TYPE_ITEM) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_score_card_data_child_view_holder, parent, false);
        return new ScoreCardDataViewHolder(view, TYPE_ITEM);
    } else if(viewType == TYPE_HEADER) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_item, parent, false);
        return new ScoreCardHeaderViewHolder(view, TYPE_HEADER);
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindChildViewHolder(ChildViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
    int viewType = getItemViewType(flatPosition);
    if(viewType == TYPE_HEADER) {
        //Currently I am passing string and binding it to ScoreCardHeaderViewHolder
        // but we can pass any data.
        ((ScoreCardHeaderViewHolder) holder).onBind("batsman");
    } else if(viewType == TYPE_ITEM) {
        //here I am getting my data from api passed to adapter and binding it to the ScoreCardDataViewHolder
        final BattingData battingData = ((BattingDataExpandable) group).getItems().get(childIndex);
        ((ScoreCardDataViewHolder) holder).onBind(battingData, group, childIndex);
    }
}

@Override
public void onBindGroupViewHolder(TeamScoreCardViewHolder holder, int flatPosition, ExpandableGroup group) {
    holder.setTeamName(group);
}

@Override
public boolean isChild(int viewType) {
    return viewType == TYPE_HEADER || viewType == TYPE_ITEM;
}

@Override
public int getChildViewType(int position, ExpandableGroup group, int childIndex) {
    if(isPositionHeader(position))
        return TYPE_HEADER;
    else
        return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
    return position == 0;
}
}