package com.yunxian.recycleview.multiexpandable.sample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.yunxian.recycleview.multiexpandable.MultiExpandableRecycleViewAdapter;
import com.yunxian.recycleview.multiexpandable.model.IExpandableItemModel;
import com.yunxian.recycleview.multiexpandable.sample.adapter.model.Employee;
import com.yunxian.recycleview.multiexpandable.sample.adapter.provider.EmployProvider;

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

/**
 * 测试RecyclerView的多级展开功能页面
 *
 * @author A Shuai
 * @email ls1110924@gmail.com
 * @date 2017/5/29 0:51
 */
public class TestMultiExpandableActivity extends AppCompatActivity {

    private static final String TAG = TestMultiExpandableActivity.class.getSimpleName();

    private final CommonCallbackListener mCommonListener = new CommonCallbackListener();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_multiexpand);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        MultiExpandableRecycleViewAdapter recycleViewAdapter = new MultiExpandableRecycleViewAdapter(this, recyclerView);
        recycleViewAdapter.registerItemViewProvider(Employee.class.getSimpleName(), EmployProvider.class);
        recycleViewAdapter.addOnExpandableItemClickListener(mCommonListener);

        recycleViewAdapter.setData(mockData());
    }

    private List<IExpandableItemModel> mockData() {

        List<IExpandableItemModel> itemModels = new ArrayList<>();

        for (int i = 0, size = 5; i < size; i++) {
            Employee employee = new Employee();
            employee.setName(String.format(Locale.getDefault(), "Name-->%d", i));
            List<Employee> children = randSubordinate(i);
            employee.setSubordinate(children);
            itemModels.add(employee);
        }
        return itemModels;

    }

    private static List<Employee> randSubordinate(int index) {
        List<Employee> itemModels = new ArrayList<>();
        for (int i = 0, size = 3; i < size; i++) {
            Employee employee = new Employee();
            employee.setName(String.format(Locale.getDefault(), "Name-->%d--%d", index, i));
            itemModels.add(employee);
        }
        return itemModels;
    }

    private class CommonCallbackListener implements MultiExpandableRecycleViewAdapter.OnExpandableItemClickListener {

        @Override
        public void onExpandableItemSelected(@NonNull IExpandableItemModel dataModel, @NonNull List<Integer> coordinate) {
            Employee employee = (Employee) dataModel;
            Log.d(TAG, "the " + employee.getName() + " has been selected!");
        }
    }

}

@@ -683,16 +683,65 @@
 .Locale;
+%0Aimport java.util.Random;%0Aimport java.util.Stack;
 %0A%0A/**%0A *
@@ -1777,24 +1777,57 @@
 ckData() %7B%0A%0A
+        final int maxLevel = 5;%0A%0A
         List
@@ -1880,16 +1880,100 @@
 ist%3C%3E();
+%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        Random random = new Random();
 %0A%0A      
@@ -2009,32 +2009,60 @@
  %3C size; i++) %7B%0A
+            stack.push(i);%0A%0A
             Empl
@@ -2171,24 +2171,80 @@
 -%3E%25d%22, i));%0A
+            employee.setExpanded(random.nextBoolean());%0A
             
@@ -2285,17 +2285,32 @@
 rdinate(
-i
+random, stack, 4
 );%0A     
@@ -2380,32 +2380,58 @@
 s.add(employee);
+%0A%0A            stack.pop();
 %0A        %7D%0A     
@@ -2514,20 +2514,117 @@
 ate(
-int index) %7B
+Random random, Stack%3CInteger%3E indexStack, int level) %7B%0A        int nodesNum = Math.max(1, random.nextInt(5));
 %0A   
@@ -2701,36 +2701,63 @@
  = 0
-, size = 3; i %3C size; i++) %7B
+; i %3C nodesNum; i++) %7B%0A            indexStack.push(i);%0A
 %0A   
@@ -2834,71 +2834,248 @@
 ame(
-String.format(Locale.getDefault(), %22Name--%3E%25d--%25d%22, index, i));
+buildNodeName(indexStack));%0A            employee.setExpanded(random.nextBoolean());%0A%0A            // %E9%80%92%E5%BD%92%E7%94%9F%E8%BE%B0%E5%AD%90%E8%8A%82%E7%82%B9%0A            if (level %3E 0) %7B%0A                employee.setSubordinate(randSubordinate(random, indexStack, level - 1));%0A            %7D%0A
 %0A   
@@ -3108,16 +3108,47 @@
 ployee);
+%0A%0A            indexStack.pop();
 %0A       
@@ -3176,32 +3176,458 @@
 mModels;%0A    %7D%0A%0A
+    private static String buildNodeName(Stack%3CInteger%3E integerStack) %7B%0A        StringBuilder stringBuilder = new StringBuilder(%22Name--%3E%22);%0A        Integer%5B%5D indexArray = integerStack.toArray(new Integer%5B0%5D);%0A        stringBuilder.append(indexArray%5B0%5D);%0A        for (int i = 1; i %3C indexArray.length; i++) %7B%0A            stringBuilder.append('-').append(indexArray%5Bi%5D);%0A        %7D%0A        return stringBuilder.toString();%0A    %7D%0A%0A
     private clas

optimize demo ui