A罩杯 发表于 2013-8-28 16:27

第四十三讲:用户界面 View(十)

>本讲内容:ExpandableListView,ExpandableListActivity 可扩展列表 二级列表 手风琴效果accordion本讲源代码下载:http://www.apkbus.com/data/attachment/forum/201104/29/175628k8wluxezwrreorer.png点击一级列表,展开下一级:http://www.apkbus.com/data/attachment/forum/201104/29/1756321tq63qsqeh6344zq.png点击二层列表(嵌套的列表)的某一项:http://www.apkbus.com/data/attachment/forum/201104/29/1756256n7nlf7866wnn60n.png   下面我们来看代码:1、新建一个项目 Lesson43_ExpandableListView2、main.xml 的内容如下:    <?xml version="1.0" encoding="utf-8"?>   
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">   
      <expandablelistview android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/android:list">   
      </expandablelistview>   
</linearlayout>请注意ExpandableListView标签中id的写法是固定的@id/android:list,因为我们这里用的是ExpandableListActivity,而ExpandableListActivity代码里写死了要寻找的UI元素是它,这和我们以前讲的很多特殊的Activity是一样的,这里再稍作提醒。3、MainActivity.java的代码如下,解释在注释里:    package basic.android.lesson43;   
   
import java.util.ArrayList;   
import java.util.HashMap;   
import java.util.List;   
import java.util.Map;   
   
import android.app.ExpandableListActivity;   
import android.os.Bundle;   
import android.view.View;   
import android.widget.ExpandableListView;   
import android.widget.SimpleExpandableListAdapter;   
import android.widget.Toast;   
   
public class MainActivity extends ExpandableListActivity {   
   
      @Override   
      public void onCreate(Bundle savedInstanceState) {   
                super.onCreate(savedInstanceState);   
                setContentView(R.layout.main);   
   
                // 准备顶层列表数据   
                List   
<map string=""><string ,="">> topList = new ArrayList</string></map>   
<map string=""><string ,="">>();   
   
                Map</string><string string="" ,=""> topMap1 = new HashMap</string><string string="" ,="">();   
                Map</string><string string="" ,=""> topMap2 = new HashMap</string><string string="" ,="">();   
                topMap1.put("month", "三月测评项");   
                topMap2.put("month", "四月测评项");   
                topList.add(topMap1);   
                topList.add(topMap2);   
   
                // 准备二层列表数据   
                List   
<list string="">   
<map><string ,="">>> nestList = new ArrayList</string></map>   
</list>   
<list string="">   
<map><string ,="">>>();   
   
                // 准备二层列表第一个子列表数据   
                List   
<map string=""><string ,="">> nestList1 = new ArrayList</string></map>   
<map string=""><string ,="">>();   
                Map</string><string string="" ,=""> nestMap1 = new HashMap</string><string string="" ,="">();   
                Map</string><string string="" ,=""> nestMap2 = new HashMap</string><string string="" ,="">();   
                Map</string><string string="" ,=""> nestMap3 = new HashMap</string><string string="" ,="">();   
                nestMap1.put("test", "看手");   
                nestMap2.put("test", "吃手");   
                nestMap3.put("test", "玩手");   
                nestList1.add(nestMap1);   
                nestList1.add(nestMap2);   
                nestList1.add(nestMap3);   
   
                // 准备二层列表第二个子列表数据   
                List   
<map string=""><string ,="">> nestList2 = new ArrayList</string></map>   
<map string=""><string ,="">>();   
                Map</string><string string="" ,=""> nestMap4 = new HashMap</string><string string="" ,="">();   
                Map</string><string string="" ,=""> nestMap5 = new HashMap</string><string string="" ,="">();   
                nestMap4.put("test", "翻身");   
                nestMap5.put("test", "辨别声音来源方位");   
                nestList2.add(nestMap4);   
                nestList2.add(nestMap5);   
   
                // 把子列表数据放入   
                nestList.add(nestList1);   
                nestList.add(nestList2);   
   
                // 准备数据匹配器   
                SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(   
                              this, //1.上下文   
                              topList, //2.顶层数据列表   
                              android.R.layout.simple_expandable_list_item_1, // 3.一层显示样式   
                              new String[]{"month"}, //4.顶层map的键   
                              new int[]{android.R.id.text1}, // 5.顶层数据显示的View ID   
                              nestList, //6.二层数据列表   
                              android.R.layout.simple_list_item_1, //7.二层显示样式   
                              new String[]{"test"}, //8.二层map的键   
                              new int[]{android.R.id.text1} //9.二层数据显示的View ID   
                              );   
   
                //设置数据匹配器   
                this.setListAdapter(adapter);   
   
      }   
   
      @Override   
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {   
                Toast.makeText(this, "嵌套列表被点击,顶层列表定位"+groupPosition+"二层列表定位"+childPosition, Toast.LENGTH_LONG).show();   
                return super.onChildClick(parent, v, groupPosition, childPosition, id);   
      }   
   
      @Override   
      public void onGroupCollapse(int groupPosition) {   
                Toast.makeText(this, "顶层列表收缩,列表定位"+groupPosition, Toast.LENGTH_LONG).show();   
                super.onGroupCollapse(groupPosition);   
      }   
   
      @Override   
      public void onGroupExpand(int groupPosition) {   
                Toast.makeText(this, "顶层列表展开,列表定位"+groupPosition, Toast.LENGTH_LONG).show();   
                super.onGroupExpand(groupPosition);   
      }   
   
}   
</string></map>   
   
</string></map>   
   
</string></map>   
</list></string></map>   
   4、编译并运行程序即可看到上面的效果。那么本节课就到这里了,Android中很多内容就像本节的ExpandableListView一样讨厌,来,我们一起鄙视一下^_^   
</div
页: [1]
查看完整版本: 第四十三讲:用户界面 View(十)