Android 如何在expandable RecyclerView中合并同名标头

Android 如何在expandable RecyclerView中合并同名标头,android,expandablelistview,expandablelistadapter,android-json,expandablerecyclerview,Android,Expandablelistview,Expandablelistadapter,Android Json,Expandablerecyclerview,我有这样一个可扩展的回收视图 我想合并相同的命名标题(如“Nov 172016”)。并在一个地方添加他们的孩子。 和图标位置必须保持不变。如何做到这一点? 以下是我的json响应: {"Table": [{"filedate":"Oct 25, 2016","clientid":4}, {"filedate":"Nov 17, 2016","clientid":4}, {"filedate":"Nov 16, 2016","clientid":4}], "Table1": [{"file

我有这样一个可扩展的回收视图



我想合并相同的命名标题(如“Nov 172016”)。并在一个地方添加他们的孩子。 和图标位置必须保持不变。如何做到这一点?

以下是我的json响应:

{"Table":

[{"filedate":"Oct 25, 2016","clientid":4},
{"filedate":"Nov 17, 2016","clientid":4},
{"filedate":"Nov 16, 2016","clientid":4}],

"Table1":

[{"filedate":"Oct 25, 2016","file1":"12.txt","category":"Category : Bank Statement"},
{"filedate":"Nov 16, 2016","file1":"Readme.docx","category":"Category : Bank Statement"},
{"filedate":"Nov 17, 2016","file1":"hts-log.txt","category":"Category : Bills"},
{"filedate":"Nov 17, 2016","file1":"cookies.txt","category":"Category : Others",},
{"filedate":"Nov 17, 2016","file1":"readme.txt","category":"Category : Invoice",}]}
  private void prepareListData() {


        // Volley's json array request object
        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        Log.d(TAG, response.toString());
//                        hidePDialog();


                        JSONObject object = null;
                        try {
                            object = new JSONObject(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        JSONArray jsonarray = null;
                        JSONArray jsonarray1 = null;
                        try {
                            jsonarray = object.getJSONArray("Table1");
                            jsonarray1 = object.getJSONArray("Table1");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                            JSONArray jsonarray1 = object.getJSONArray("Table2");
                        for (int i = 0; i < jsonarray.length(); i++) {
                            try {

                                JSONObject obj = jsonarray.getJSONObject(i);
//                                    Movie movie = new Movie();
//                                    movie.setFiledate(obj.getString("filedate"));
                                String str = obj.optString("filedate").trim();


                                Log.d("test", str);

                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, str));

//                                    Toast.makeText(getApplicationContext(), lth, Toast.LENGTH_LONG).show();

                                    for (int j = 0; j < jsonarray1.length(); j++) {

                                        try {

                                            JSONObject obj1 = jsonarray1.getJSONObject(j);
                                            String str1 = obj1.optString("filedate").trim();
                                            String str2 = obj1.optString("file1").trim();
                                            String str3 = obj1.optString("filename").trim();
                                            String str4 = obj1.optString("category").trim();
                                                Toast.makeText(getApplicationContext(), "server data respone", Toast.LENGTH_LONG).show();
                                                Toast.makeText(getApplicationContext(), "test"+str1, Toast.LENGTH_LONG).show();
                                            if (str == str1) {
                                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, str4));
                                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, str2));
                                            }
                                                Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();

                                            //if condition


                                        } catch (JSONException e) {
//                                            Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                                        }

                                    }

                                // adding movie to movies array



                            } catch (JSONException e) {
                                Log.e("gdshfsjkg", "JSON Parsing error: " + e.getMessage());
                            }

                        }                        Log.d("test", String.valueOf(data));




                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
//                        adapterheader.notifyDataSetChanged();
                        recyclerview.setAdapter(new ExpandableListAdapter(data));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                VolleyLog.d(TAG, "Error: " + error.getMessage());
//                hidePDialog();

            }
        }){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(CLIENT, "4");
                return params;
            }
        };
        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(stringRequest);
    }
以下是我获取json响应的代码:

{"Table":

[{"filedate":"Oct 25, 2016","clientid":4},
{"filedate":"Nov 17, 2016","clientid":4},
{"filedate":"Nov 16, 2016","clientid":4}],

"Table1":

[{"filedate":"Oct 25, 2016","file1":"12.txt","category":"Category : Bank Statement"},
{"filedate":"Nov 16, 2016","file1":"Readme.docx","category":"Category : Bank Statement"},
{"filedate":"Nov 17, 2016","file1":"hts-log.txt","category":"Category : Bills"},
{"filedate":"Nov 17, 2016","file1":"cookies.txt","category":"Category : Others",},
{"filedate":"Nov 17, 2016","file1":"readme.txt","category":"Category : Invoice",}]}
  private void prepareListData() {


        // Volley's json array request object
        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        Log.d(TAG, response.toString());
//                        hidePDialog();


                        JSONObject object = null;
                        try {
                            object = new JSONObject(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        JSONArray jsonarray = null;
                        JSONArray jsonarray1 = null;
                        try {
                            jsonarray = object.getJSONArray("Table1");
                            jsonarray1 = object.getJSONArray("Table1");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                            JSONArray jsonarray1 = object.getJSONArray("Table2");
                        for (int i = 0; i < jsonarray.length(); i++) {
                            try {

                                JSONObject obj = jsonarray.getJSONObject(i);
//                                    Movie movie = new Movie();
//                                    movie.setFiledate(obj.getString("filedate"));
                                String str = obj.optString("filedate").trim();


                                Log.d("test", str);

                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, str));

//                                    Toast.makeText(getApplicationContext(), lth, Toast.LENGTH_LONG).show();

                                    for (int j = 0; j < jsonarray1.length(); j++) {

                                        try {

                                            JSONObject obj1 = jsonarray1.getJSONObject(j);
                                            String str1 = obj1.optString("filedate").trim();
                                            String str2 = obj1.optString("file1").trim();
                                            String str3 = obj1.optString("filename").trim();
                                            String str4 = obj1.optString("category").trim();
                                                Toast.makeText(getApplicationContext(), "server data respone", Toast.LENGTH_LONG).show();
                                                Toast.makeText(getApplicationContext(), "test"+str1, Toast.LENGTH_LONG).show();
                                            if (str == str1) {
                                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, str4));
                                                data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, str2));
                                            }
                                                Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();

                                            //if condition


                                        } catch (JSONException e) {
//                                            Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                                        }

                                    }

                                // adding movie to movies array



                            } catch (JSONException e) {
                                Log.e("gdshfsjkg", "JSON Parsing error: " + e.getMessage());
                            }

                        }                        Log.d("test", String.valueOf(data));




                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
//                        adapterheader.notifyDataSetChanged();
                        recyclerview.setAdapter(new ExpandableListAdapter(data));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                VolleyLog.d(TAG, "Error: " + error.getMessage());
//                hidePDialog();

            }
        }){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(CLIENT, "4");
                return params;
            }
        };
        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(stringRequest);
    }
private void prepareListData(){
//Volley的json数组请求对象
StringRequest StringRequest=新建StringRequest(Request.Method.POST,REGISTER\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//Log.d(TAG,response.toString());
//hidePDialog();
JSONObject对象=null;
试一试{
对象=新的JSONObject(响应);
}捕获(JSONException e){
e、 printStackTrace();
}
JSONArray JSONArray=null;
JSONArray jsonarray1=null;
试一试{
jsonarray=object.getJSONArray(“表1”);
jsonarray1=object.getJSONArray(“表1”);
}捕获(JSONException e){
e、 printStackTrace();
}
//JSONArray jsonarray1=object.getJSONArray(“表2”);
for(int i=0;i
阿达彼得班

public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int HEADER = 0;
    public static final int CHILD = 1;

    private List<Item> data;

    public ExpandableListAdapter(List<Item> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
        View view = null;
        Context context = parent.getContext();
        float dp = context.getResources().getDisplayMetrics().density;
        int subItemPaddingLeft = (int) (18 * dp);
        int subItemPaddingTopAndBottom = (int) (5 * dp);
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            case HEADER:

                view = inflater.inflate(R.layout.list_header, parent, false);
                ListHeaderViewHolder header = new ListHeaderViewHolder(view);
                return header;
            case CHILD:
                view = inflater.inflate(R.layout.listchild, parent, false);
                ListChildViewHolder child = new ListChildViewHolder(view);
                return child;
        }
        return null;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Item item = data.get(position);
        switch (item.type) {
            case HEADER:
                final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
                itemController.refferalItem = item;
                itemController.header_title.setText(item.text);
                if (item.invisibleChildren == null) {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                } else {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                }
                itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.invisibleChildren == null) {
                            item.invisibleChildren = new ArrayList<Item>();
                            int count = 0;
                            int pos = data.indexOf(itemController.refferalItem);
                            while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
                                item.invisibleChildren.add(data.remove(pos + 1));
                                count++;
                            }
                            notifyItemRangeRemoved(pos + 1, count);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                        } else {
                            int pos = data.indexOf(itemController.refferalItem);
                            int index = pos + 1;
                            for (Item i : item.invisibleChildren) {
                                data.add(index, i);
                                index++;
                            }
                            notifyItemRangeInserted(pos + 1, index - pos - 1);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                            item.invisibleChildren = null;
                        }
                    }
                });
                break;
            case CHILD:
                boolean showIcon = position > 1 && getItemViewType(position) == CHILD && getItemViewType(position - 2) == HEADER;

                final ListChildViewHolder itemController1 = (ListChildViewHolder) holder;
                itemController1.refferalItem = item;
                itemController1.header_title1.setText(item.text);
                itemController1.btn_expand_toggle1.setVisibility((showIcon) ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).type;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
        public TextView header_title;
        public ImageView btn_expand_toggle;
        public Item refferalItem;

        public ListHeaderViewHolder(View itemView) {
            super(itemView);
            header_title = (TextView) itemView.findViewById(R.id.header_title);
            btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);
        }
    }
    private static class ListChildViewHolder extends RecyclerView.ViewHolder {
        public TextView header_title1;
        public ImageView btn_expand_toggle1;
        public Item refferalItem;

        public ListChildViewHolder(View itemView) {
            super(itemView);
            header_title1 = (TextView) itemView.findViewById(R.id.header_title1);
            btn_expand_toggle1 = (ImageView) itemView.findViewById(R.id.btn_expand_toggle1);
        }
    }
    public static class Item {
        public int type;
        public String text;
        public List<Item> invisibleChildren;

        public Item() {
        }

        public Item(int type, String text) {
            this.type = type;
            this.text = text;
        }
    }
}
公共类ExpandableListAdapter扩展了RecyclerView.Adapter{ 公共静态最终整数头=0; 公共静态final int CHILD=1; 私人名单数据; 公共ExpandableListAdapter(列表数据){ 这个数据=数据; } @凌驾 public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int类型){ 视图=空; Context=parent.getContext(); float dp=context.getResources().getDisplayMetrics().density; int子项paddingleft=(int)(18*dp); int子项paddingtopandbooth=(int)(5*dp); LayoutFlater充气器=(LayoutFlater)parent.getContext().getSystemService(Context.LAYOUT\u充气器\u服务); 开关(类型){ 案例标题: 视图=充气机。充气(R.layout.list_标题,父项,false); 利斯特