实现这样的效果需要自定义一个Adapter,自定义的Adapter继承BaseExpandableListAdapter,重写getGroupView和
getChildView方法时实例化自己的布局文件就可以了。下面是实现代码:
主布局文件 main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<ExpandableListView
android:id="@+id/elist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
/>
</LinearLayout>
子视图布局文件 child_layout.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="30dp"
>
<ImageView
android:id="@+id/civ"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ren"
android:padding="5dp"
/>
<TextView
android:id="@+id/ctv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="5dp"
android:textColor="#000000"
/>
</LinearLayout>
分组视图布局文件 group_layout.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/gtv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:textColor="#000000"
/>
<ImageView
android:id="@+id/giv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="@drawable/jia"
/>
</RelativeLayout>
自定义适配器 MyElistAdapter.java
publicclass MyElistAdapter extends BaseExpandableListAdapter {
// 分组数据
private String[] group = { "A组", "B组", "C组", "D组" };
private String[][] child = { { "A01", "A02", "A03" },
{ "B01", "B02", "B03" }, { "C01", "C02", "C03" },
{ "D04", "D05", "D06" } };
private Context mContext;
public MyElistAdapter(Context mContext) {
super();
this.mContext = mContext;
}
@Override
public int getGroupCount() {
return group.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return child[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return group[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
returnchildPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 实例化布局文件
RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)
.inflate(R.layout.group_layout, null);
ImageView iv = (ImageView) glayout.findViewById(R.id.giv);
// 判断分组是否展开,分别传入不同的图片资源
if (isExpanded) {
iv.setImageResource(R.drawable.jian);
} else {
iv.setImageResource(R.drawable.jia);
}
TextView tv = (TextView) glayout.findViewById(R.id.gtv);
tv.setText(this.getGroup(groupPosition).toString());
return glayout;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// 实例化布局文件
LinearLayout clayout = (LinearLayout) LayoutInflater.from(mContext)
.inflate(R.layout.child_layout, null);
TextView tv = (TextView) clayout.findViewById(R.id.ctv);
tv.setText(getChild(groupPosition, childPosition).toString());
return clayout;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity.java
publicclass ExpandableTestActivity extends Activity {
private ExpandableListView elistview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
elistview = (ExpandableListView) findViewById(R.id.elist);
//这里要把系统自带的图标去掉
elistview.setGroupIndicator(null);
elistview.setAdapter(new ElistAdapter(this));
// elistview.setChildDivider(null);
// elistview.setDivider(null);
}
}
- 大小: 35 KB
分享到:
相关推荐
Android自定义ExpandableListView实现二级列表
安卓Android源码——基于自定义适配器的ExpandableListView源码.zip
Android实现自定义适配器的ExpandableListView示例,准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2",准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2",...
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据
ExpandableListView中group和child自定义视图带CheckBox,实现child全选、部分选中功能,同时实现类似邮件群发的用户选择功能,选中的用户在EditText中显示,点击后移除. 更新: 1. ExpandableListView勾选后生成的...
android中ExpandableListView增加自定义组件,点击ExpandableListView自动滚动到顶部
ExpandableListView中group和child自定义视图带CheckBox,实现child全选、部分选中功能,同时实现类似邮件群发的用户选择功能,选中的用户在EditText中显示,点击后移除. 更新: 1. ExpandableListView勾选后生成的...
自定义expandablelistview一级标题,并实现类似radiobutton单选效果
android客户端获取服务器端数据在ExpandableListView中显示,类似QQ列表,这是简单的客户/服务器模式,可以了解客户端和服务器交换数据的过程。
Android 基于自定义适配器的ExpandableListView源码
自定义Android中的ExpandableListView group及child的显示方式
实现二级列表下拉,如listview,自定义适配器,初学者可以学习下
基于自定义适配器的ExpandableListView源码.zip
Android 基于自定义适配器的ExpandableListView源码.zip源码资源下载Android 基于自定义适配器的ExpandableListView源码.zip源码资源下载
可扩展自定义列表视图自定义 ExpandableListview 的基本用法专为@Sudheesh Mohan 设计的项目如果您有任何问题,请发送邮件至: ► 你是安卓程序员吗? 加入我们的 Android 开发者团队
Android 基于自定义适配器的ExpandableListView源码.rar
Android程序研发源码Android 基于自定义适配器的ExpandableListView源码.zip
Android 基于自定义适配器的ExpandableListView源码.zip
Android基于自定义适配器的ExpandableListView源码.zip