有時在寫程式時,我們希望一個listview能展開其下的子類目,在android中可以通過使用ExpandAbleListView來實現,只需要在代碼裡為ExpandAbleListView設定一個ExpandAbleLIstAdapter的資料來源即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:childIndicator="@drawable/icon"
></ExpandableListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:childIndicator="@drawable/icon"
></ExpandableListView>
</LinearLayout>
public class ExpandableListViewTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//建立一個BaseExpandableListAdapter對象
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
int[] logos = new int[]
{
R.drawable.sunyz_1,
R.drawable.sunyz_6,
R.drawable.sunyz_7
};
private String[] CDs = new String[]
{ "風箏", "完美的一天", "是時候"};
private String[][] songs = new String[][]
{
{ "綠光", "不是真的愛我", "愛情字典", "練習" },
{ "Honey Honey", "心愿", "明天晴天", "隱形人" },
{ "愚人的國度", "是時候" , "世說心語" }
};
//擷取指定組位置、指定子清單項目處的子清單項目資料
@Override
public Object getChild(int groupPosition, int childPosition)
{
return songs[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return songs[groupPosition].length;
}
private TextView getTextView()
{
//用於實現條目的虛擬列表的基類. 這裡的列表沒有空間的定義。 例如,該類的子類可以以網格的形式、走馬燈的形式顯示,或者作為堆棧等等
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64); //設定寬和高
TextView textView = new TextView(ExpandableListViewTest.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//該方法決定每個子選項的外觀
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//擷取指定組位置處的組資料
@Override
public Object getGroup(int groupPosition)
{
return CDs[groupPosition];
}
@Override
public int getGroupCount()
{
return CDs.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//該方法決定每個組選項的外觀
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
ExpandableListView expandListView = (ExpandableListView)
findViewById(R.id.list);
expandListView.setAdapter(adapter);
}
}
摘自 snoopy的專欄