package cn.etzmico.expandablelistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mGroupData;
private int mExpandedGroupLayout;
private int mCollapsedGroupLayout;
private String[] mGroupFrom;
private int[] mGroupTo;
private List<? extends List<? extends Map<String, ?>>> mChildData;
private int mChildLayout;
private int mLastChildLayout;
private String[] mChildFrom;
private int[] mChildTo;
private LayoutInflater mInflater;
public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData,
int groupLayout, String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout,
String[] childFrom, int[] childTo) {
this(context, groupData, groupLayout, groupLayout, groupFrom, groupTo, childData,
childLayout, childLayout, childFrom, childTo);
}
public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData,
int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout,
String[] childFrom, int[] childTo) {
this(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo,
childData, childLayout, childLayout, childFrom, childTo);
}
public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData,
int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout,
int lastChildLayout, String[] childFrom, int[] childTo) {
mGroupData = groupData;
mExpandedGroupLayout = expandedGroupLayout;
mCollapsedGroupLayout = collapsedGroupLayout;
mGroupFrom = groupFrom;
mGroupTo = groupTo;
mChildData = childData;
mChildLayout = childLayout;
mLastChildLayout = lastChildLayout;
mChildFrom = childFrom;
mChildTo = childTo;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getChild(int groupPosition, int childPosition) {
return mChildData.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* Instantiates a new View for a child.
* @param isLastChild Whether the child is the last child within its group.
* @param parent The eventual parent of this new View.
* @return A new child View
*/
public View newChildView(boolean isLastChild, ViewGroup parent) {
return mInflater.inflate((isLastChild) ? mLastChildLayout : mChildLayout, parent, false);
}
public int getChildrenCount(int groupPosition) {
return mChildData.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return mGroupData.get(groupPosition);
}
public int getGroupCount() {
return mGroupData.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* Instantiates a new View for a group.
* @param isExpanded Whether the group is currently expanded.
* @param parent The eventual parent of this new View.
* @return A new group View
*/
public View newGroupView(boolean isExpanded, ViewGroup parent) {
return mInflater.inflate((isExpanded) ? mExpandedGroupLayout : mCollapsedGroupLayout,
parent, false);
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
// --------------------------------------------------------------------------------------//
private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
int len = to.length;
boolean isBound = false;
for (int i = 0; i < len; i++) {
final View v = view.findViewById(to[i]);
if (v!=null) {
final Object _data = data.get(from[i]);
String text = _data == null ? "" : data.toString();
if (text == null) {
text = "";
}
if (mViewBinder != null) {//���Binder��Ϊ��,ʹ��Binder���д���
isBound = mViewBinder.setViewValue(v, data.get(from[i]), text);
}
if (!isBound) {//���Binder���,ʹ��ԭ���ķ������д���
TextView _v = (TextView)v;
_v.setText((String)data.get(from[i]));
}
}
}
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parent);
} else {
v = convertView;
}
bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
return v;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newChildView(isLastChild, parent);
} else {
v = convertView;
}
bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom, mChildTo);
return v;
}
public void setViewBinder(ViewBinder mViewBinder) {
this.mViewBinder = mViewBinder;
}
/**
* �ṩ��ͼ��Ⱦ�İ���
* @author Atomic
*/
public static interface ViewBinder {
boolean setViewValue(View view, Object data, String textRepresentation);
}
// private View createViewFromResource(int position, View convertView,
// ViewGroup parent, int resource) {
// View v;
// if (convertView == null) {
// v = mInflater.inflate(resource, parent, false);
// } else {
// v = convertView;
// }
//
// bindView(position, v);
//
// return v;
// }
// private void bindView(int position, View view) {
// final Map dataSet = mData.get(position);
// if (dataSet == null) {
// return;
// }
//
// final ViewBinder binder = mViewBinder;
// final String[] from = mFrom;
// final int[] to = mTo;
// final int count = to.length;
//
// for (int i = 0; i < count; i++) {
// final View v = view.findViewById(to[i]);
// if (v != null) {
// final Object data = dataSet.get(from[i]);
// String text = data == null ? "" : data.toString();
// if (text == null) {
// text = "";
// }
//
// boolean bound = false;
// if (binder != null) {
// bound = binder.setViewValue(v, data, text);
// }
//
// if (!bound) {
// if (v instanceof Checkable) {
// if (data instanceof Boolean) {
// ((Checkable) v).setChecked((Boolean) data);
// } else if (v instanceof TextView) {
// // Note: keep the instanceof TextView check at the bottom of these
// // ifs since a lot of views are TextViews (e.g. CheckBoxes).
// setViewText((TextView) v, text);
// } else {
// throw new IllegalStateException(v.getClass().getName() +
// " should be bound to a Boolean, not a " +
// (data == null ? "<unknown type>" : data.getClass()));
// }
// } else if (v instanceof TextView) {
// // Note: keep the instanceof TextView check at the bottom of these
// // ifs since a lot of views are TextViews (e.g. CheckBoxes).
// setViewText((TextView) v, text);
// } else if (v instanceof ImageView) {
// if (data instanceof Integer) {
// setViewImage((ImageView) v, (Integer) data);
// } else {
// setViewImage((ImageView) v, text);
// }
// } else {
// throw new IllegalStateException(v.getClass().getName() + " is not a " +
// " view that can be bounds by this SimpleAdapter");
// }
// }
// }
// }
// }
}
【Android】自定义可扩展列表ExpandableListView



在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展示具有层次结构的数据。这个控件的特殊之处在于它可以展开和折叠子项,从而更好地管理屏幕空间,为用户提供更加直观的操作体验。本篇文章将深入探讨如何在Android中自定义可扩展列表`ExpandableListView`。 我们要理解`ExpandableListView`的基本结构。它由两部分组成:父项(Group)和子项(Child)。每个父项可以包含多个子项,用户点击父项时,其对应的子项会显示或隐藏。要使用`ExpandableListView`,我们需要创建两个适配器:一个用于父项,另一个用于子项。通常,我们会继承`BaseExpandableListAdapter`来实现这两个适配器。 1. **自定义适配器** - **父项适配器**:继承`BaseExpandableListAdapter`,重写`getGroupCount()`、`getChildrenCount(int groupPosition)`、`getGroup(int groupPosition)`、`getChild(int groupPosition, int childPosition)`等方法,提供父项和子项的数量以及数据。 - **子项适配器**:同样继承`BaseExpandableListAdapter`,但主要关注子项数据的获取,如`getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)`,在这个方法中,我们可以自定义子项视图的布局和内容。 2. **绑定数据** 在创建了适配器之后,我们需要将数据绑定到`ExpandableListView`。通过`setGroupList()`和`setChildList()`方法,将父项和子项的数据列表传递给适配器,然后调用`expandGroup()`或`collapseGroup()`方法来控制特定父项的展开和折叠状态。 3. **事件监听** 我们可以设置`OnGroupClickListener`和`OnChildClickListener`监听器来处理父项和子项的点击事件。在监听器中,我们可以根据需要执行相应的操作,例如跳转到其他界面或者更新数据。 4. **自定义视图** 自定义`ExpandableListView`的外观主要是通过修改适配器中的`getView()`方法来实现。这里可以对父项和子项的视图进行个性化设计,包括设置背景、文字颜色、字体大小等。同时,可以利用`convertView`参数来实现视图复用,提高性能。 5. **动画效果** Android提供了内置的动画效果,如展开/折叠动画,可以通过设置`setAnimationDuration()`来调整动画时长。也可以自定义动画效果,通过`ExpandableListView`的`onGroupExpanded()`和`onGroupCollapsed()`回调方法实现。 6. **优化与性能** `ExpandableListView`在处理大量数据时可能会出现性能问题,因此在实际应用中,我们应尽量使用高效的数据结构,如`SparseArray`存储数据,避免不必要的内存分配。此外,适配器中的`convertView`机制是提升性能的关键,确保正确复用已存在的视图对象。 7. **其他功能** `ExpandableListView`还支持头像、子项分隔线等特性,可以根据需求添加。例如,通过`setGroupIndicator()`可以设置展开/折叠指示器,通过`setDivider()`和`setDividerHeight()`可以设置子项之间的分隔线。 `ExpandableListView`是Android中处理层级数据的强大工具,通过自定义适配器和视图,我们可以创建出满足各种需求的可扩展列表。在实践中,我们要注意性能优化,提供流畅的用户体验。结合事件监听和动画效果,可以进一步提升应用的交互性和吸引力。











































































- 1

- 粉丝: 129
- 资源: 46
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 三菱Q系列PLC与ABB机器人综合项目案例:含注释程序、触摸屏画面及智能模块应用,三菱Q系列PLC与ABB机器人大型项目程序案例:含注释、触摸屏画面及智能模块应用,三菱Q系列PLC 大型项目程序案例
- Furion-C#资源
- 西门子200smart PLC自写PID功能块:突破限制,轻松实现项目快速开发与调试,西门子200smart PLC自定义PID功能块开发与应用挑战解析:解决PID参数管理难题,提升项目开发与调试效率
- JFinal-PHP资源
- xe-utils-JavaScript资源
- 改进粒子群算法在微电网多目标优化调度中的应用:环保与经济模型研究,改进粒子群算法下的微电网多目标优化调度策略:经济环保模型求解,2基于改进粒子群算法的微电网多目标优化调度 以微电网的运行成本、环境保护
- NRCE-TEST-计算机二级资源
- 毕业设计-毕业设计资源
- 三菱Q06UDV系列PLC与威纶通触摸屏驱动的锂电池电芯组盘机全自动控制系统,三菱Q06UDV系列PLC与威纶通触摸屏驱动的锂电池电芯组盘机全自动控制系统,三菱Q06UDV系列PLC,威纶通触摸屏,锂
- IR2-Net-ACM资源
- TC264__2022-智能车资源
- rlcard-机器学习开发资源
- MATLAB-轮腿仿真(哈工程建模)-matlab仿真资源
- dag_scheduler-scratch资源
- deepseek-DeepSeek资源
- 智慧园区管理系统-活动资源



- 1
- 2
- 3
- 4
前往页