Android中ExpandableListView的用法实例
程序员文章站
2022-04-30 23:00:45
本文实例讲述了android中expandablelistview的用法,expandablelistview是android中可以实现下拉list的一个控件,具体的实现方...
本文实例讲述了android中expandablelistview的用法,expandablelistview是android中可以实现下拉list的一个控件,具体的实现方法如下:
首先:在layout的xml文件中定义一个expandablelistview
复制代码 代码如下:
<linearlayout
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<expandablelistview
android:id="@+id/expandablelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</linearlayout>
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<expandablelistview
android:id="@+id/expandablelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</linearlayout>
定义两个list,用来存放控件中group/child中的string
复制代码 代码如下:
private list<string> grouparray;
private list<list<string>> childarray;
private list<list<string>> childarray;
对这两个list进行初始化,并插入一些数据
复制代码 代码如下:
grouparray = new arraylist<string>();
childarray = new arraylist<list<string>>();
grouparray.add("第一行");
grouparray.add("第二行");
list<string> temparray = new arraylist<string>();
temparray.add("第一条");
temparray.add("第二条");
temparray.add("第三条");
for(int index = 0; index <grouparray.size(); ++index)
{
childarray.add(temparray);
}
childarray = new arraylist<list<string>>();
grouparray.add("第一行");
grouparray.add("第二行");
list<string> temparray = new arraylist<string>();
temparray.add("第一条");
temparray.add("第二条");
temparray.add("第三条");
for(int index = 0; index <grouparray.size(); ++index)
{
childarray.add(temparray);
}
定义expandablelistview的adapter
复制代码 代码如下:
//expandablelistview的adapter
public class expandableadapter extends baseexpandablelistadapter
{
activity activity;
public expandableadapter(activity a)
{
activity = a;
}
public object getchild(int groupposition, int childposition)
{
return childarray.get(groupposition).get(childposition);
}
public long getchildid(int groupposition, int childposition)
{
return childposition;
}
public int getchildrencount(int groupposition)
{
return childarray.get(groupposition).size();
}
public view getchildview(int groupposition, int childposition,
boolean islastchild, view convertview, viewgroup parent)
{
string string = childarray.get(groupposition).get(childposition);
return getgenericview(string);
}
// group method stub
public object getgroup(int groupposition)
{
return grouparray.get(groupposition);
}
public int getgroupcount()
{
return grouparray.size();
}
public long getgroupid(int groupposition)
{
return groupposition;
}
public view getgroupview(int groupposition, boolean isexpanded,
view convertview, viewgroup parent)
{
string string = grouparray.get(groupposition);
return getgenericview(string);
}
// view stub to create group/children 's view
public textview getgenericview(string string)
{
// layout parameters for the expandablelistview
abslistview.layoutparams layoutparams = new abslistview.layoutparams(
viewgroup.layoutparams.fill_parent, 64);
textview text = new textview(activity);
text.setlayoutparams(layoutparams);
// center the text vertically
text.setgravity(gravity.center_vertical | gravity.left);
// set the text starting position
text.setpadding(36, 0, 0, 0);
text.settext(string);
return text;
}
public boolean hasstableids()
{
return false;
}
public boolean ischildselectable(int groupposition, int childposition)
{
return true;
}
}
public class expandableadapter extends baseexpandablelistadapter
{
activity activity;
public expandableadapter(activity a)
{
activity = a;
}
public object getchild(int groupposition, int childposition)
{
return childarray.get(groupposition).get(childposition);
}
public long getchildid(int groupposition, int childposition)
{
return childposition;
}
public int getchildrencount(int groupposition)
{
return childarray.get(groupposition).size();
}
public view getchildview(int groupposition, int childposition,
boolean islastchild, view convertview, viewgroup parent)
{
string string = childarray.get(groupposition).get(childposition);
return getgenericview(string);
}
// group method stub
public object getgroup(int groupposition)
{
return grouparray.get(groupposition);
}
public int getgroupcount()
{
return grouparray.size();
}
public long getgroupid(int groupposition)
{
return groupposition;
}
public view getgroupview(int groupposition, boolean isexpanded,
view convertview, viewgroup parent)
{
string string = grouparray.get(groupposition);
return getgenericview(string);
}
// view stub to create group/children 's view
public textview getgenericview(string string)
{
// layout parameters for the expandablelistview
abslistview.layoutparams layoutparams = new abslistview.layoutparams(
viewgroup.layoutparams.fill_parent, 64);
textview text = new textview(activity);
text.setlayoutparams(layoutparams);
// center the text vertically
text.setgravity(gravity.center_vertical | gravity.left);
// set the text starting position
text.setpadding(36, 0, 0, 0);
text.settext(string);
return text;
}
public boolean hasstableids()
{
return false;
}
public boolean ischildselectable(int groupposition, int childposition)
{
return true;
}
}
最后,给定义好的expandablelistview添加上adapter
复制代码 代码如下:
expandablelistview expandablelistview = (expandablelistview)findviewbyid(r.id.expandablelistview);
expandablelistview.setadapter(new expandableadapter(main.this));
expandablelistview.setadapter(new expandableadapter(main.this));
希望本文所述对大家的android程序设计有所帮助。