在Android的多级ExpandableListViewAndroid、ExpandableListView

2023-09-04 03:54:29 作者:姐,嫁我可好

我想从一个给定的未知大小也不类别的水平列表中创建一个目录树。所以我想创建一个一般的扩展列表可以包含2+水平的数量。 总的想法是要添加到谁拥有孩子的另一个ExpandableListView在它的布局每一个孩子。问题是,第二级不会打开,它看起来像它在儿童或一些渲染它。下面是一些屏幕截图的结果:

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:

下面就是它的外观像以前一样打开

Here's how it's look like before opening

开放的第一个选项后(这应该是空的)

And after opening the first option: (It's supposed to be empty)

和现在打开第二个:(有4个孩子的,其中一人有他自己的孩子的)

And now open the second one: (Has 4 childs and one of them have childs of his own)

正如你所看到的第三个选项看起来像它的被渲染就可以了另一种选择什么的,点击打开后:

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...

这里的code:

CatAdapter.java:(延伸BaseExpandableListAdapter) http://pastebin.com/6yUTkMbJ

CatAdapter.java: (extends BaseExpandableListAdapter) http://pastebin.com/6yUTkMbJ

Category.java: http://pastebin.com/E7yWwpna

Category.java: http://pastebin.com/E7yWwpna

catitem.xml: http://pastebin.com/G5MPT3Ua

catitem.xml: http://pastebin.com/G5MPT3Ua

用法: 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!

编辑:

我结束了做一个自定义视图完成这个任务,谢谢大家对你的答案!

I ended up making a custom view for this task, thank you all for your answers!

推荐答案

我认为这个问题是在你的 getChildView()方法:

I believe the problem is in your getChildView() method:

    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;
    }

当你遇到一个'扩展'孩子,你还在膨胀 R.layout.catitem 并添加新的 ELV 吧。因为 catitem RelativeLayout的,你不加任何参数校准,每个视图被放置在顶部 - 左拐角处,覆盖任何其他人的存在。

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 有一个垂直的的LinearLayout 作为其根。这应该$ P $重叠孩子的标题pvent他们,但我不能保证孩子的孩子会不会仍然重叠。这是一个简单的变化,不过,值得一试。

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.

此外,从文档的 ExpandableListView

Also, from the docs for ExpandableListView:

请注意:不能使用值WRAP_CONTENT为Android:XML中的ExpandableListView的layout_height属性,如果父母的大小也没有严格规定的(例如,如果父母是滚动型你不能指定WRAP_CONTENT,因为它也可以是任何长度,但你可以使用WRAP_CONTENT如果ExpandableListView家长有一个特定的大小,如100像素。

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.

这是说,在XML中,所以我不知道这是否意味着适用于code与否。在我看来,他们会使用相同的机制,所以它可能是值得研究的。我不知道你如何去设置一个最大尺寸为母公司。你可能能够测量一个项目的大小,和儿童的数量相乘。你得把利润/分离器考虑进去,所以它可能不会是简单的。

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.

如果不工作,你可能需要直接推出自己的ViewGroup。它不应该太难从头开始实现它,只是别忘了尝试采取来看回收利用(当前您的方法不反正做的)。

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).