java Android 中的多级 ExpandableListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17772348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-level ExpandableListView in Android
提问by UnTraDe
I'm trying to create a category tree from a given unknown-size nor level list of categories. So I'm trying to create a general expandable list which can contain 2+ number of levels. The general idea is to add to every child who has childs another ExpandableListView in it's layout. The problem is that the second level wont open, it looks like it rendered it over the child or something. Here's some screen-shots of the result:
我正在尝试从给定的未知大小或级别的类别列表创建类别树。所以我试图创建一个可以包含 2+ 个级别的通用可扩展列表。一般的想法是在每个有孩子的孩子的布局中添加另一个 ExpandableListView。问题是第二层打不开,看起来像是把它渲染在孩子身上。这是结果的一些屏幕截图:
Here's how it's look like before opening
这是打开前的样子
And after opening the first option: (It's supposed to be empty)
打开第一个选项后:(它应该是空的)
And now open the second one: (Has 4 childs and one of them have childs of his own)
现在打开第二个:(有4个孩子,其中一个有自己的孩子)
As you can see the third option looks like it's been rendered another option or something on it, after clicking to open it:
如您所见,单击打开后,第三个选项看起来像是呈现了另一个选项或其他内容:
It's does nothing except change the state of the arrow. At least it tries to open it...
除了改变箭头的状态外,它什么都不做。至少它试图打开它......
Here's the code:
这是代码:
CatAdapter.java: (extends BaseExpandableListAdapter) http://pastebin.com/6yUTkMbJ
CatAdapter.java:(扩展 BaseExpandableListAdapter) http://pastebin.com/6yUTkMbJ
Category.java: http://pastebin.com/E7yWwpna
类别.java:http: //pastebin.com/E7yWwpna
catitem.xml: http://pastebin.com/G5MPT3Ua
catitem.xml:http://pastebin.com/G5MPT3Ua
The usage: http://pastebin.com/Wk0FqJhn
用法:http: //pastebin.com/Wk0FqJhn
Sorry for the long question, I was trying to be clear as possible.
抱歉问了这么长的问题,我试图尽可能清楚。
Thanks in advance! Sorry for my bad english!
提前致谢!对不起,我的英语不好!
Edit:
编辑:
I ended up making a custom view for this task, thank you all for your answers!
我最终为此任务制作了自定义视图,谢谢大家的回答!
采纳答案by Geobits
I believe the problem is in your getChildView()
method:
我相信问题出在你的getChildView()
方法上:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.catitem, parent, false);
TextView textView_catName = (TextView)convertView.findViewById(R.id.textView_catName);
Category current = categories.get(groupPosition).childs.get(childPosition);
textView_catName.setText(groupPosition + " , " + childPosition);
if(current.childs.size() > 0 ) {
ExpandableListView elv = new ExpandableListView(context);
elv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
elv.setAdapter(new CatAdapter(context, current.childs));
((ViewGroup)convertView).addView(elv);
}
return convertView;
}
When you encounter an 'expandable' child, you are still inflating R.layout.catitem
and adding your new elv
to it. Since the catitem
is a RelativeLayout
and you don't add any parameters for alignment, each view is placed at the top-left corner, overlaying whatever else is there.
当你遇到一个“可扩展”的孩子时,你仍然在膨胀并向它R.layout.catitem
添加你的新孩子elv
。由于catitem
是 aRelativeLayout
并且您没有添加任何对齐参数,因此每个视图都放置在左上角,覆盖那里的任何其他内容。
You may want to try changing R.layout.catitem
to have a vertical LinearLayout
as its root. This should prevent them from overlapping the child's title, but I can't guarantee that the children's children will not still overlap. It's an easy change, though, and worth a shot.
您可能想尝试更改R.layout.catitem
为以垂直LinearLayout
为根。这应该可以防止他们重叠孩子的标题,但我不能保证孩子的孩子仍然不会重叠。不过,这是一个简单的更改,值得一试。
Also, from the docs for ExpandableListView
:
此外,来自以下文档ExpandableListView
:
Note: You cannot use the value wrap_content for the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also not strictly specified (for example, if the parent were ScrollView you could not specify wrap_content since it also can be any length. However, you can use wrap_content if the ExpandableListView parent has a specific size, such as 100 pixels.
注意:如果未严格指定父级的大小(例如,如果父级是 ScrollView,则不能指定 wrap_content,因为它也可以是任意长度),则不能在 XML 中为 ExpandableListView 的 android:layout_height 属性使用值 wrap_content。但是,如果 ExpandableListView 父级具有特定大小(例如 100 像素),则可以使用 wrap_content。
That says "in XML", so I'm not sure if it means to apply to code or not. It seems to me that they'd use the same mechanism, so it might be worth looking into. I'm not sure how you'd go about setting a maximum size for the parent. You may be able to measure the size of one item, and multiply by the number of children. You'd have to take margins/separators into account, so it may not be simple.
那是“在 XML 中”,所以我不确定这是否意味着适用于代码。在我看来,他们会使用相同的机制,因此可能值得研究。我不确定您将如何为父级设置最大尺寸。您可以测量一件物品的大小,然后乘以孩子的数量。您必须考虑边距/分隔符,所以它可能并不简单。
If that doesn't work, you may need to roll your own ViewGroup directly. It shouldn't be too hard to implement it from scratch, just don't forget to try to take advantage of view recycling(which your current method doesn't do anyway).
如果这不起作用,您可能需要直接滚动自己的 ViewGroup。从头开始实现它应该不会太难,只是不要忘记尝试利用视图回收(您当前的方法无论如何都不会这样做)。
回答by Geobits
I was have same situation where I had to use ExpandableList
and at each level I needed to have groupPosition
and childPosition
but using multi-level ExpandableList
, It was not possible to get accurate groupPosition
at second level data. So that, I used another approach. I have given a demo link which I used in a project.
我遇到了同样的情况,我必须使用ExpandableList
并且在我需要的每个级别上都有groupPosition
,childPosition
但是使用 multi-level ExpandableList
,不可能groupPosition
在二级数据上获得准确的数据。所以,我使用了另一种方法。我已经给出了我在项目中使用的演示链接。
1) In this library, you need to bind parent child relation in advance before you bind your actual data.
2) In your Layout
, top most layout must be LinearLayout.
Please refer header.xml
, groups.xml
and childs.xml
in demo link.
1)在这个库中,你需要在绑定你的实际数据之前提前绑定父子关系。2) 在您的Layout
, 最上面的布局必须是LinearLayout.
请参考header.xml
,groups.xml
并childs.xml
在演示链接中。
Demo Link: MultiLevelTreeView
演示链接:MultiLevelTreeView
Thanks.
谢谢。