Android 自定义 ExpandableListView
2011-10-27 15:23
183 查看
Android中有一控件是ExpandableListView,比ListView更高级,ExpandableListView的效果很实用,比如因为需要查看一堆文件的目录结构或者开发像QQ好友那样的界面,就应该使用Expandablelistview。
本文最终效果如下:
首先是Activity代码,实际开发中数据(包括父item,子item及图片,Expandablelistview布局也可以轻易更改)可以很方便的从数据库或网络动态取得,本文方便起见数据就先定死了。
public class C_ExpandableListView extends Activity {
ExpandableListView expandableList;
public String[] str1 = { "我的好友", "陌生人", "黑名单","yilee","good","哈哈哈" };
public String[] str2 = { "我了个去", "哈哈", "蟹", "XX", "我去" };
public String[] str3 = { "哈哈", "蟹", "XX", "我去" };
public String[] str4 = { "我了个去", "蟹", "XX", "我去" };
public String[] str5 = { "我了个去", "蟹", "XX", "我去" };
public String[] str6 = { "yilee", "哈哈", "蟹", "XX", "我去" };
public String[] str7 = { "哈哈", "蟹", "XX", "我去" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandableList = (ExpandableListView) C_ExpandableListView.this
.findViewById(R.id.ExpandableListView01);
expandableList.setAdapter(new TreeViewAdapter(this));
}
public class TreeViewAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private LayoutInflater inflater1;
public TreeViewAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
this.inflater1 = LayoutInflater.from(c);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View myView = inflater1.inflate(R.layout.cc, null);
final int a=groupPosition;
final int b=childPosition;
myView.setBackgroundResource(R.drawable.child);
TextView textview = (TextView) myView
.findViewById(R.id.TextView001);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("yileeeee", "groupPosition=
"+a);
Log.i("yileeeee", "childPosition=
"+b);
}
});
if(groupPosition==0){
textview.setText(str2[childPosition]);
}else if(groupPosition==1){
textview.setText(str3[childPosition]);
}else if(groupPosition==2){
textview.setText(str4[childPosition]);
}else if(groupPosition==3){
textview.setText(str5[childPosition]);
}else if(groupPosition==4){
textview.setText(str6[childPosition]);
}else if(groupPosition==5){
textview.setText(str7[childPosition]);
}
return myView;
}
@Override
public int getChildrenCount(int groupPosition) {
if(groupPosition==0){
return str2.length;
}else if(groupPosition==1){
return str3.length;
}else if(groupPosition==2){
return str4.length;
}else if(groupPosition==3){
return str5.length;
}else if(groupPosition==4){
return str6.length;
}else {
return str7.length;
}
}
@Override
public Object getGroup(int groupPosition) {
return "dd";
}
@Override
public int getGroupCount() {
return str1.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View myView = inflater.inflate(R.layout.dd, null);
myView.setBackgroundResource(R.drawable.group);
TextView textview = (TextView) myView.findViewById(R.id.TextView01);
textview.setText(str1[groupPosition]);
return myView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
其中类TreeViewAdapter是我们的自定义Adapter,继承自BaseExpandableListAdapter。
getChildrenCount比较麻烦,因为么个子item数目并不一样,可以把子数据放入一个String三维数组,这样只需return str[position],返回的便是子item的数据,样在getChildView在也应该这样设置:textview.setText(str[position][childPosition]);
我们需要三个布局文件,一个是activity里面是ExpandableListView
<ExpandableListView android:id="@+id/ExpandableListView01"
android:layout_width="200dp" android:layout_height="fill_parent"
android:layout_x="20dip" android:layout_y="30dip"
android:groupIndicator="@null"android:childDivider="@drawable/child_divider"
android:clickable="true" android:scrollbarAlwaysDrawHorizontalTrack="true">
</ExpandableListView>
第二个是描述父item的布局文件
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16px"
android:gravity="center">
</TextView>
最后一个是描述子item的
<ImageView android:id="@+id/ImageView01"
android:layout_height="30dp" android:layout_width="30dp"
android:background="@drawable/head">
</ImageView>
<TextView android:id="@+id/TextView001" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="center">
</TextView>
这样就可以轻易实现ExpandableListAdapter 高度自定义了。
本文最终效果如下:
首先是Activity代码,实际开发中数据(包括父item,子item及图片,Expandablelistview布局也可以轻易更改)可以很方便的从数据库或网络动态取得,本文方便起见数据就先定死了。
public class C_ExpandableListView extends Activity {
ExpandableListView expandableList;
public String[] str1 = { "我的好友", "陌生人", "黑名单","yilee","good","哈哈哈" };
public String[] str2 = { "我了个去", "哈哈", "蟹", "XX", "我去" };
public String[] str3 = { "哈哈", "蟹", "XX", "我去" };
public String[] str4 = { "我了个去", "蟹", "XX", "我去" };
public String[] str5 = { "我了个去", "蟹", "XX", "我去" };
public String[] str6 = { "yilee", "哈哈", "蟹", "XX", "我去" };
public String[] str7 = { "哈哈", "蟹", "XX", "我去" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandableList = (ExpandableListView) C_ExpandableListView.this
.findViewById(R.id.ExpandableListView01);
expandableList.setAdapter(new TreeViewAdapter(this));
}
public class TreeViewAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private LayoutInflater inflater1;
public TreeViewAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
this.inflater1 = LayoutInflater.from(c);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View myView = inflater1.inflate(R.layout.cc, null);
final int a=groupPosition;
final int b=childPosition;
myView.setBackgroundResource(R.drawable.child);
TextView textview = (TextView) myView
.findViewById(R.id.TextView001);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("yileeeee", "groupPosition=
"+a);
Log.i("yileeeee", "childPosition=
"+b);
}
});
if(groupPosition==0){
textview.setText(str2[childPosition]);
}else if(groupPosition==1){
textview.setText(str3[childPosition]);
}else if(groupPosition==2){
textview.setText(str4[childPosition]);
}else if(groupPosition==3){
textview.setText(str5[childPosition]);
}else if(groupPosition==4){
textview.setText(str6[childPosition]);
}else if(groupPosition==5){
textview.setText(str7[childPosition]);
}
return myView;
}
@Override
public int getChildrenCount(int groupPosition) {
if(groupPosition==0){
return str2.length;
}else if(groupPosition==1){
return str3.length;
}else if(groupPosition==2){
return str4.length;
}else if(groupPosition==3){
return str5.length;
}else if(groupPosition==4){
return str6.length;
}else {
return str7.length;
}
}
@Override
public Object getGroup(int groupPosition) {
return "dd";
}
@Override
public int getGroupCount() {
return str1.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View myView = inflater.inflate(R.layout.dd, null);
myView.setBackgroundResource(R.drawable.group);
TextView textview = (TextView) myView.findViewById(R.id.TextView01);
textview.setText(str1[groupPosition]);
return myView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
其中类TreeViewAdapter是我们的自定义Adapter,继承自BaseExpandableListAdapter。
getChildrenCount比较麻烦,因为么个子item数目并不一样,可以把子数据放入一个String三维数组,这样只需return str[position],返回的便是子item的数据,样在getChildView在也应该这样设置:textview.setText(str[position][childPosition]);
我们需要三个布局文件,一个是activity里面是ExpandableListView
<ExpandableListView android:id="@+id/ExpandableListView01"
android:layout_width="200dp" android:layout_height="fill_parent"
android:layout_x="20dip" android:layout_y="30dip"
android:groupIndicator="@null"android:childDivider="@drawable/child_divider"
android:clickable="true" android:scrollbarAlwaysDrawHorizontalTrack="true">
</ExpandableListView>
第二个是描述父item的布局文件
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16px"
android:gravity="center">
</TextView>
最后一个是描述子item的
<ImageView android:id="@+id/ImageView01"
android:layout_height="30dp" android:layout_width="30dp"
android:background="@drawable/head">
</ImageView>
<TextView android:id="@+id/TextView001" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="center">
</TextView>
这样就可以轻易实现ExpandableListAdapter 高度自定义了。
相关文章推荐
- Android自定义ViewGroup(二)——带悬停标题的ExpandableListView
- Android---自定义带CheckBox的ExpandableListView实现
- android--自定义ExpandableListView+隐藏指示器图片+防数据显示混乱
- android中ExpandableListView 使用自定义Adapter的方法
- Android 自定义 ExpandableListView
- Android自定义ExpandableListView
- 站在巨人的肩膀上---重新自定义 android- ExpandableListView 收缩类,实现列表的可收缩扩展
- Android关于自定义ExpandableListView样式
- android ExpandableListView的group是一个自定义的布局,折叠图标覆盖布局上的部分内容
- Android 自定义 ExpandableListView
- Android自定义ExpandableListView
- Android 自定义下拉刷新ExpandableListView
- android ExpandableListView 删除 出现重复数据
- android ExpandableListView
- Android中自定义ListView无法响应OnItemClickListener中的onItemClick方法问题解决方案
- 一步一步学android之控件篇——ListView自定义显示数据格式
- Android_ExpandableListView_子item响应点击事件
- android 基础-自定义listView的实现 adapter 方法解析
- Android ExpandableListView每组选一个值,并展开列表
- 详解Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)