ExpandableListView控件
来源:互联网 发布:淘宝客优惠券采集软件 编辑:程序博客网 时间:2024/06/11 19:52
转自:http://www.2cto.com/kf/201110/106616.html
这一章我们来学习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);
//设置ImageView要显示的对象ID
imageView.setImageResource(image[groupPosition]);
//将ImageView加到线性布局中
ll.addView(imageView);
//使用自定义文本框
TextView textView = getTextView();
//设置文本框里显示内容
textView.setText(getGroup(groupPosition).toString());
//将TextView加到线性布局中
ll.addView(textView);
return ll;
}
//返回父控件的ID
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//返回父控件的总数
@Override
public int getGroupCount()
{
return ability.length;
} www.2cto.com
//取得父控件对象
@Override
public Object getGroup(int groupPosition)
{
return item[groupPosition];
}
//取得子控件的数量
@Override
public int getChildrenCount(int groupPosition)
{
return ability[groupPosition].length;
}
//取得子控件的视图
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
//使用自定义TextView控件
TextView textView = getTextView();
//设置自定义TextView控件的内容
textView.setText(getChild(groupPosition, childPosition)
.toString());
return textView;
}
//取得子控件的ID
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
//取得子控件的对象
@Override
public Object getChild(int groupPosition, int childPosition)
{
return ability[groupPosition][childPosition];
}
//自定义文本框
public TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ButtonDemoActivity.this);
textView.setLayoutParams(lp);
textView.setPadding(20, 0, 0, 0);
//设置TextView控件为向左,水平居中对齐
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
return textView;
}
};
explandListView.setAdapter(adapter);
}
private void updateText(String string)
{
// 将文本信息设置给TextView控件显示出来
text.setText(string);
}
}
其实ExpandableListView控件使用起来还是比较方便的,只要学会了BaseExpandableListAdapter()的使用方法就可以了,当然,这里面的布局都是在代码部分生成的,为了扩展方便,也可以使用Layout.xml布局文件来生成.这就留到课后,同学们自己想吧?主要是使用LayoutInflater,好了,今天这一课结束了,谢谢
参考资料:
http://developer.android.com/reference/android/widget/BaseExpandableListAdapter.html
http://www.cnblogs.com/over140/archive/2011/03/18/1987666.html
http://www.cnblogs.com/tianyake/archive/2012/02/19/2358588.html
http://www.oschina.net/question/157182_45920
http://www.2cto.com/kf/201201/115871.html
http://www.oschina.net/question/157182_45920
- ExpandableListView控件
- ExpandableListView控件
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- ExpandableListView 添加其他控件
- ExpandableListView控件使用
- ExpandableListView控件的使用
- ExpandableListView控件基本使用
- android控件-ExpandableListView
- 树形ExpandableListView控件使用
- android控件之expandableListView
- Android ExpandableListView 展开列表控件
- Android之ExpandableListView控件使用
- [控件]支持索引的ExpandableListView
- android 控件 ExpandableListView 二级listview
- 半角 全角转换
- OpenJWeb网上商城开发文档及源代码
- CompareTo Semantics
- 走进SQL Server 2005 tempdb数据库
- 基础总结篇之三:Activity的task相关 (转载liuhe688)
- ExpandableListView控件
- OpenGL的坐标系
- CentOS设置默认启动命令行(不启动图形界面)
- 【Anychart】自动保存flash图片到指定目录下。
- 表格的渲染模型
- IT人的境界
- ORA-00600错误及其解决方案
- local static
- 关于msdn中NtQuerySystemInformation函数说明