ZNDS智能电视网 推荐当贝市场

TV应用下载 / 资源分享区

软件下载 | 游戏 | 讨论 | 电视计算器

综合交流 / 评测 / 活动区

交流区 | 测硬件 | 网站活动 | Z币中心

新手入门 / 进阶 / 社区互助

新手 | 你问我答 | 免费刷机救砖 | ROM固件

查看: 16423|回复: 0
上一主题 下一主题
[教程]

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

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-28 16:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
>
本讲内容:ExpandableListView,ExpandableListActivity 可扩展列表 二级列表 手风琴效果accordion
本讲源代码下载:
  
点击一级列表,展开下一级:
  
点击二层列表(嵌套的列表)的某一项:
   
下面我们来看代码:
1、新建一个项目 Lesson43_ExpandableListView
2、main.xml 的内容如下:
   
  1. <?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的代码如下,解释在注释里:   
  1. 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

上一篇:android的消息推送
下一篇:android_下拉菜单.docx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

Archiver|新帖|标签|软件|Sitemap|ZNDS智能电视网 ( 苏ICP备2023012627号 )

网络信息服务信用承诺书 | 增值电信业务经营许可证:苏B2-20221768 丨 苏公网安备 32011402011373号

GMT+8, 2025-7-29 05:33 , Processed in 0.064437 second(s), 13 queries , Redis On.

Powered by Discuz!

监督举报:report#znds.com (请将#替换为@)

© 2007-2025 ZNDS.Com

快速回复 返回顶部 返回列表