{"id":1001,"date":"2017-03-28T21:37:27","date_gmt":"2017-03-28T13:37:27","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1001"},"modified":"2017-03-28T21:37:27","modified_gmt":"2017-03-28T13:37:27","slug":"android%e5%bc%80%e5%8f%91%e4%b9%8bexpandablelistview%e5%88%86%e7%bb%84%e5%88%97%e8%a1%a8%e7%9a%84%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/03\/28\/2137271001.html","title":{"rendered":"Android\u5f00\u53d1\u4e4bExpandableListView(\u5206\u7ec4\u5217\u8868)\u7684\u7b80\u5355\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/03\/device-2017-03-28-211759.png\" alt=\"\" width=\"360\" height=\"640\" class=\"alignnone size-full wp-image-1002\" \/><\/p>\n<p>2\u3001\u57fa\u672c\u8bbe\u8ba1<br \/>\n2.1\u3001\u6839\u636e\bMVC\u7684\u8bbe\u8ba1\u539f\u7406\uff0c\u5206\u7ec4\u5217\u8868\u5206\u4e09\u4e2a\u90e8\u5206\u6765\u9610\u8ff0\uff1b<br \/>\n2.1.1\u3001\u63a7\u5236\u5668C\uff0c\u4f7fActivity\u7ee7\u627f\u81eaExpandableListActivity\uff1b<br \/>\n2.1.2\u3001\u89c6\u56feView\uff0c\u5206\u7ec4\u5217\u8868\u4e2d\u89c6\u56fe\u4e3b\u8981\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c\u4e00\u4e2a\u662f\u8868\u5934\u90e8\u5206\uff0c\u4e00\u4e2a\u662f\u8868\u4e2d\u5355\u5143\u683c\uff1b\u521b\u5efa\u4e24\u4e2aXML\u6587\u4ef6\u5206\u522b\u63cf\u8ff0\uff1b<br \/>\n2.2\u3001\u6570\u636e\u6a21\u578bModel\uff0c\u6700\u7b80\u5355\u7684\u5b9e\u73b0\u4f7f\u7528SimpleExpandableListAdapter\u6784\u9020\u65b9\u6cd5\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u9002\u914d\u5668\uff0c\u521d\u59cb\u5316\u51e0\u4e2aList\u63d0\u4f9b\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\uff1b<br \/>\n2.3\u3001\u53c2\u7167 <a href=\"http:\/\/www.yusian.com\/blog\/android\/2017\/03\/22\/143628.html\" target=\"_blank\">Android\u5f00\u53d1\u57fa\u7840\u63a7\u4ef6ListView\u7684\u4f7f\u7528<\/a><\/p>\n<p>3\u3001\u5173\u952e\u4ee3\u7801\uff1a<!--more--><br \/>\nactivity_main.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\"com.yusian.expandablelist.MainActivity\">\n\n    <ExpandableListView\n        android:id=\"@id\/android:list\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\">\n    <\/ExpandableListView>\n\n    <TextView\n        android:id=\"@id\/android:empty\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"No Data\"\/>\n<\/LinearLayout><\/pre>\n<p>child.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:orientation=\"vertical\" android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\">\n\n    <TextView\n        android:id=\"@+id\/child_to\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"44dp\"\n        android:gravity=\"center_vertical\"\n        android:paddingLeft=\"20dp\"\n        android:text=\"No data\"\/>\n\n<\/LinearLayout><\/pre>\n<p>group.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:orientation=\"vertical\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\">\n\n    <TextView\n        android:id=\"@+id\/group_to\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"44dp\"\n        android:gravity=\"center_vertical\"\n        android:paddingLeft=\"30dp\"\n        android:text=\"No Data\"\n        android:background=\"#C0C0C0\"\/>\n<\/LinearLayout><\/pre>\n<p>MainActivity<\/p>\n<pre line=\"1\" lang=\"java\">package com.yusian.expandablelist;\n\nimport android.app.ExpandableListActivity;\nimport android.os.Bundle;\nimport android.widget.SimpleExpandableListAdapter;\n\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\n\npublic class MainActivity extends ExpandableListActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        \/\/ 1\u3001\u5206\u7ec4\u5217\u8868\u7ec4\u5934\u6570\u636e(\u6240\u6709\u7ec4)\n        \/\/ [{group:Group-1},{group:Group-2}]\n        List<Map<String, String>> groups = new ArrayList<>();\n        Map<String, String> group1 = new HashMap<>();\n        group1.put(\"group\", \"Group-1\");\n        Map<String, String> group2 = new HashMap<>();\n        group2.put(\"group\", \"Group-2\");\n        groups.add(group1);\n        groups.add(group2);\n\n\n        \/\/ 2\u3001\u5206\u7ec4\u5217\u8868\uff0c\u5404\u79cd\u5217\u8868\u6570\u636e\n        \/\/ 2.1\u3001\u7b2c\u4e00\u7ec4\u5217\u8868\u6570\u636e\n        \/\/ [{child:Child1-->Data1},{child:Child1-->Data2}]\n        List<Map<String, String>> child1 = new ArrayList<>();\n        Map<String, String> child1Data1 = new HashMap<>();\n        child1Data1.put(\"child\", \"Child1-->Data1\");\n        Map<String, String> child1Data2 = new HashMap<>();\n        child1Data2.put(\"child\", \"Child1-->Data2\");\n        child1.add(child1Data1);\n        child1.add(child1Data2);\n\n        \/\/ 2.2\u3001\u7b2c\u4e8c\u7ec4\u5217\u8868\u6570\u636e\n        \/\/ [{child:Child2-->Data1},{child:Child2-->Data2}]\n        List<Map<String, String>> child2 = new ArrayList<>();\n        Map<String, String> child2Data1 = new HashMap<>();\n        child2Data1.put(\"child\", \"Child2-->Data1\");\n        Map<String, String> child2Data2 = new HashMap<>();\n        child2Data2.put(\"child\", \"Child2-->Data2\");\n        child2.add(child2Data1);\n        child2.add(child2Data2);\n\n        \/\/ 2.3\u3001\u5c06\u4e24\u5217\u8868\u6570\u636e\u6dfb\u52a0\u5230\u4e00\u4e2aList\u4e2d\n        \/*[\n            [{child:Child1-->Data1},{child:Child1-->Data2}],\n            [{child:Child2-->Data1},{child:Child2-->Data2}]\n        ]**\/\n        List<List<Map<String, String>>> childs = new ArrayList<>();\n        childs.add(child1);\n        childs.add(child2);\n\n        \/\/ 3\u3001\u521b\u5efa\u4e00\u4e2aAdapter\u5bf9\u8c61\uff0c\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\u5e76\u8d4b\u503c\u7ed9\u5f53\u524dListView\n        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this,\n                groups, R.layout.group, new String[]{\"group\"}, new int[]{R.id.group_to},\n                childs, R.layout.child, new String[]{\"child\"}, new int[]{R.id.child_to});\n        setListAdapter(adapter);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe 2\u3001\u57fa\u672c\u8bbe\u8ba1 2.1\u3001\u6839\u636e\bMVC\u7684\u8bbe\u8ba1\u539f\u7406\uff0c\u5206\u7ec4\u5217\u8868\u5206\u4e09\u4e2a\u90e8\u5206\u6765\u9610\u8ff0\uff1b 2.1.1\u3001\u63a7\u5236\u5668C\uff0c\u4f7fActivity\u7ee7\u627f\u81eaExpandableListActivity\uff1b 2.1.2\u3001\u89c6\u56feView\uff0c\u5206\u7ec4\u5217\u8868\u4e2d\u89c6\u56fe\u4e3b\u8981\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c\u4e00\u4e2a\u662f\u8868\u5934\u90e8\u5206\uff0c\u4e00\u4e2a\u662f\u8868\u4e2d\u5355\u5143\u683c\uff1b\u521b\u5efa\u4e24\u4e2aXML\u6587\u4ef6\u5206\u522b\u63cf\u8ff0\uff1b 2.2\u3001\u6570\u636e\u6a21\u578bModel\uff0c\u6700\u7b80\u5355\u7684\u5b9e\u73b0\u4f7f\u7528SimpleExpandableListAdapter\u6784\u9020\u65b9\u6cd5\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u9002\u914d\u5668\uff0c\u521d\u59cb\u5316\u51e0\u4e2aList\u63d0\u4f9b\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\uff1b 2.3\u3001\u53c2\u7167 Android\u5f00\u53d1\u57fa\u7840\u63a7\u4ef6ListView\u7684\u4f7f\u7528 3\u3001\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33],"class_list":["post-1001","post","type-post","status-publish","format-standard","hentry","category-android","tag-33"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1001"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1001\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}