> 本講內(nèi)容:ExpandableListView,ExpandableListActivity 可擴(kuò)展列表 二級(jí)列表 手風(fēng)琴效果accordion 本講源代碼下載: 點(diǎn)擊一級(jí)列表,展開下一級(jí): 點(diǎn)擊二層列表(嵌套的列表)的某一項(xiàng): 下面我們來看代碼: 1、新建一個(gè)項(xiàng)目 Lesson43_ExpandableListView 2、main.xml 的內(nèi)容如下: - <?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>
復(fù)制代碼 請(qǐng)注意ExpandableListView標(biāo)簽中id的寫法是固定的@id/android:list,因?yàn)槲覀冞@里用的是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);
// 準(zhǔn)備頂層列表數(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", "三月測(cè)評(píng)項(xiàng)");
topMap2.put("month", "四月測(cè)評(píng)項(xiàng)");
topList.add(topMap1);
topList.add(topMap2);
// 準(zhǔn)備二層列表數(shù)據(jù)
List
<list string="">
<map><string ,="">>> nestList = new ArrayList</string></map>
</list>
<list string="">
<map><string ,="">>>();
// 準(zhǔn)備二層列表第一個(gè)子列表數(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);
// 準(zhǔn)備二層列表第二個(gè)子列表數(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);
// 準(zhǔn)備數(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, "嵌套列表被點(diǎn)擊,頂層列表定位"+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>
復(fù)制代碼 4、編譯并運(yùn)行程序即可看到上面的效果。那么本節(jié)課就到這里了,Android中很多內(nèi)容就像本節(jié)的ExpandableListView一樣討厭,來,我們一起鄙視一下^_^
</div |