Android[初级教程]第八章 ExpandableListView控件
这一章我们来学习ExpandableListView控件,ExpandableListView就是可扩展列表控件,还是拿西游记来说,里面唐僧师徙都各有各的本领,每个人的本领都不一样,放一起显示肯定不适合,这里我们就用ExpandableListView控件将他们显示出来.
看main.xml
<?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">
<TextView android:text="西游记各主人公能力" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<ExpandableListView android:layout_height="wrap_content"
android:id="@+id/expandableListView" android:layout_width="match_parent"></ExpandableListView>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="@string/hello"
android:id="@+id/text"></TextView>
</LinearLayout>
还是跟前几次一样,定义了一个ExpandableListView控件,下面是java源代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ButtonDemoActivity extends Activity
{
private TextView text = null;
private int[] image = { R.drawable.tangseng, R.drawable.wukong,
R.drawable.bajie, R.drawable.shaseng };
private String[] item = { "唐僧", "孙悟空 ", "猪八戒", "沙和尚" };
private String[][] ability = { { "会念紧箍咒", "会说阿弥陀佛" },
{ "会七十二变", "会打妖精", "会腾云驾雾" }, { "会偷懒", "会睡觉" }, { "会挑担子" } };
private ExpandableListView explandListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 通过ID查找到main.xml中的TextView控件
text = (TextView) findViewById(R.id.text);
// 通过ID查找到main.xml中的ExpandableListView控件
explandListView = (ExpandableListView) findViewById(R.id.expandableListView);
//设置ExpandableListView适配器
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
//处理子项目的单击事件
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition)
{
String str = item[groupPosition]
+ ability[groupPosition][childPosition];
updateText(str);
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
//返回父项目的视图控件
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
//新建一个线性布局
LinearLayout ll = new LinearLayout(ButtonDemoActivity.this);
// 设置布局样式为Horizontal
ll.setOrientation(0);
//设置布局左边距为50像素
ll.setPadding(50, 0, 0, 0);
//新建一个ImageView对象
ImageView imageView = new ImageView(ButtonDemoActivity.this);
&nb
补充:移动开发 , Android ,