package top.wgx.swprecyclerview.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;

import java.util.ArrayList;
import java.util.List;

import top.wgx.swprecyclerview.adapter.a.BaseRecyclerHolder;

/**
 * Created by Administrator on 2017/9/1 0001.
 */

public abstract class BRExpandAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {
    private boolean mDefault = true;//默认关闭
    private static final int TYPE_GROUP = 1011;
    private static final int TYPE_ITEM = 1012;
    private List<T> mList = null;
    int layout_group = 0, layout_item = 0;
    private boolean[] misClose;

    public BRExpandAdapter() {
     init();
//        this.misClose = new boolean[0];
    }

    public BRExpandAdapter(int layout_group,int layout_item){
        this.layout_group = layout_group;
        this.layout_item = layout_item;
        init();
    }

    protected void init() {
        this.mList = new ArrayList<>();
    }

    public boolean getDefault() {
        return mDefault;
    }

    public void setmDefault(boolean mDefault) {
        this.mDefault = mDefault;
    }

    public void setList(List<T> list) {
        mList.clear();
        mList.addAll(list);
        misClose = new boolean[list.size()];
        for (int i = 0; i < mList.size(); i++) {
            misClose[i] = mDefault;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        int[] groupAndChild = getGroupAndChild(position);
        if (groupAndChild[1]<0){
            return TYPE_GROUP;
        }else {
            return TYPE_ITEM;
        }
    }

    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_GROUP) {
            return new GroupHolder(View.inflate(parent.getContext(), layout_group, null));
        } else{
            return new ChildHolder(View.inflate(parent.getContext(), layout_item, null));
        }
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, final int position) {
        final int[] x = getGroupAndChild(position);
        if (getItemViewType(position) == TYPE_GROUP) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switchState(x[0]);
                    Log.e("---------","--#"+x[0]+"#--#"+x[1]+"#---#"+getItemCount());

                }
            });
            bindGroup(holder.itemView.getContext(), holder, mList.get(x[0]), x[0]);
        } else {
            bindChild(holder.itemView.getContext(), holder, mList.get(x[0]), x[0], x[1]);
        }
    }

    private void switchState(int i) {
        if (misClose[i]) {
            closeGroup(i);
        } else {
            expandGroup(i);
        }
    }

    public void expandGroup(int i) {
        misClose[i] = true;
        notifyDataSetChanged();
    }

    public void closeGroup(int i) {
        misClose[i] = false;
        notifyDataSetChanged();
    }


    private int[] getGroupAndChild(int position) {
        int[] x = new int[]{-1, -1};
        int count = -1;
        for (int i = 0; i < getGroupCount(); i++) {
            count++;
            x[0] = i;
            if (count == position) {
                return x;
            } else {
                if (misClose[x[0]]){
                    continue;
                }
                int childrenCount = getChildrenCount(mList.get(x[0]),i);
                if (count < position && position <= count + childrenCount) {
                    x[1] = position - count - 1;
                    return x;
                } else {
                    count += childrenCount;
                }
            }
        }
        return x;
    }


    @Override
    public int getItemCount() {
        int count = 0;
        for (int i = 0; i < mList.size(); i++) {
            count += (!misClose[i] ? getChildrenCount(mList.get(i),i) : 0) + 1;
        }
        return count;
    }


    public int getGroupCount() {
        return mList.size();
    }
    protected abstract int getChildrenCount(T t,int group) ;
    protected abstract void bindGroup(Context context, BaseRecyclerHolder bHolder, T t, int group);
    protected abstract void bindChild(Context context, BaseRecyclerHolder bHolder,T t, int group, int child);




    class GroupHolder extends BaseRecyclerHolder {

        public GroupHolder(View itemView) {
            super(itemView);
        }
    }

    class ChildHolder extends BaseRecyclerHolder {

        public ChildHolder(View itemView) {
            super(itemView);
        }
    }

}

@@ -836,24 +836,27 @@
  %7B%0A     
+   
 init();%0A
 //      
@@ -851,50 +851,8 @@
 ();%0A
-//        this.misClose = new boolean%5B0%5D;%0A
     
@@ -898,16 +898,17 @@
 t_group,
+ 
 int layo
@@ -915,16 +915,17 @@
 ut_item)
+ 
 %7B%0A      
@@ -1674,11 +1674,14 @@
 d%5B1%5D
-%3C
+ %3C 
 0)
+ 
 %7B%0A  
@@ -1718,16 +1718,17 @@
        %7D
+ 
 else %7B%0A 
@@ -2013,16 +2013,17 @@
   %7D else
+ 
 %7B%0A      
@@ -2558,39 +2558,50 @@
 --%22,
+ 
 %22--#%22
-+
+ + 
 x%5B0%5D
-+
+ + 
 %22#--#%22
-+
+ + 
 x%5B1%5D
-+
+ + 
 %22#---#%22
-+
+ + 
 getI
@@ -3545,16 +3545,17 @@
 e%5Bx%5B0%5D%5D)
+ 
 %7B%0A      
@@ -3665,16 +3665,17 @@
 t(x%5B0%5D),
+ 
 i);%0A    
@@ -4148,16 +4148,17 @@
 .get(i),
+ 
 i) : 0) 
@@ -4266,24 +4266,25 @@
 ze();%0A    %7D%0A
+%0A
     protecte
@@ -4319,16 +4319,17 @@
 unt(T t,
+ 
 int grou
@@ -4334,10 +4334,10 @@
 oup)
- 
 ;
+%0A
 %0A   
@@ -4433,16 +4433,17 @@
 group);%0A
+%0A
     prot
@@ -4516,16 +4516,17 @@
 bHolder,
+ 
 T t, int
@@ -4547,18 +4547,16 @@
 ild);%0A%0A%0A
-%0A%0A
     clas

更新：增加BRExpandAdapter 可折叠列表，基于recyclerview 实现a