首頁 收藏 QQ群
 網(wǎng)站導航

ZNDS智能電視網(wǎng) 推薦當貝市場

TV應用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 14366|回復: 0
上一主題 下一主題
[教程]

第四十三講:用戶界面 View(十)

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:27 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
>
本講內(nèi)容:ExpandableListView,ExpandableListActivity 可擴展列表 二級列表 手風琴效果accordion
本講源代碼下載:
  
點擊一級列表,展開下一級:
  
點擊二層列表(嵌套的列表)的某一項:
   
下面我們來看代碼:
1、新建一個項目 Lesson43_ExpandableListView
2、main.xml 的內(nèi)容如下:
   
  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);   
       
                    // 準備頂層列表數(shù)據(jù)   
                    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);   
       
                    // 準備二層列表數(shù)據(jù)   
                    List   
    <list string="">   
    <map><string ,="">>> nestList = new ArrayList</string></map>   
    </list>   
    <list string="">   
    <map><string ,="">>>();   
       
                    // 準備二層列表第一個子列表數(shù)據(jù)   
                    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);   
       
                    // 準備二層列表第二個子列表數(shù)據(jù)   
                    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);   
       
                    // 把子列表數(shù)據(jù)放入   
                    nestList.add(nestList1);   
                    nestList.add(nestList2);   
       
                    // 準備數(shù)據(jù)匹配器   
                    SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(   
                                    this, //1.上下文   
                                    topList, //2.頂層數(shù)據(jù)列表   
                                    android.R.layout.simple_expandable_list_item_1, // 3.一層顯示樣式   
                                    new String[]{"month"}, //4.頂層map的鍵   
                                    new int[]{android.R.id.text1}, // 5.頂層數(shù)據(jù)顯示的View ID   
                                    nestList, //6.二層數(shù)據(jù)列表   
                                    android.R.layout.simple_list_item_1, //7.二層顯示樣式   
                                    new String[]{"test"}, //8.二層map的鍵   
                                    new int[]{android.R.id.text1} //9.二層數(shù)據(jù)顯示的View ID   
                                    );   
       
                    //設(shè)置數(shù)據(jù)匹配器   
                    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、編譯并運行程序即可看到上面的效果。那么本節(jié)課就到這里了,Android中很多內(nèi)容就像本節(jié)的ExpandableListView一樣討厭,來,我們一起鄙視一下^_^   
</div

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

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡信息服務信用承諾書 | 增值電信業(yè)務經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-21 03:33 , Processed in 0.060778 second(s), 13 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復 返回頂部 返回列表