我是android的新手,我想实现一个ListView,如下图所示。似乎expandableListView可能满足我的要求,但是我想要的只有一个Item 4
孩子并且可以扩展,“第1项”只是用户可以单击但不能扩展的选项。我阅读了可扩展列表视图的文档,但如何实现它还不够清楚。有人可以提出一些实施建议吗?太感谢了
但是我想要的只是第4项有孩子并且可以扩展
如果Item没有孩子,则不会被扩展。您可以像这样成像:
public class Item {
private String title;
private List<Option> children; // if size = 0 won't be expanded
}
这是本机的行为,ExpandableListView
因此,如果任何Item没有子项,则隐式不会扩展,但是您需要为组(项)和子项(选项)都设置OnClickListener:
public boolean onChildClick(ExpandableListView parent, View row,
int groupPosition, int childPosition, long id) {
// callback method after click for Options
return false;
}
public boolean onGroupClick(ExpandableListView parent, View row,
int groupPosition, long id) {
/** if you are using BaseAdapter subclass implementation **/
adapter.getGroup(groupPosition)).getChildren() == null) {
// item doesn't have children
}
else {
// has children
}
通常,如果您想获得对ListView的更多控制权,我建议您实现自己的ListAdapter子类,可以使用BaseExpandableListAdapter。
List的出现只是有关适当样式的布局的问题,例如,删除带有
android:groupIndicator="@null"
@Roberto Lombardini提到的问题:
此解决方案的一个小问题是,即使组没有子代,也会出现指示组视图状态(已扩展或未扩展)的灰色小三角形。我错了吗?
如果只想为带有子项的项设置灰色三角形,则需要使用以下getGroupView()
方法在自定义适配器中对其进行修复:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// inflating View etc.
Item item = items.get(groupPosition);
if (item != null) {
// if Item has children
if (item.getChildren() != null && !item.getChildren().isEmpty()) {
// set children indicator to VISIBLE
imageView.setVisibility(ImageView.VISIBLE);
}
// if Item doesn't have children
else {
// set children indicator to GONE
imageView.setVisibility(ImageView.GONE);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句