ExpandableList getChildView每个孩子跑两次 [英] ExpandableList getChildView running twice per child

查看:183
本文介绍了ExpandableList getChildView每个孩子跑两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了和 ExpandableList ,我分配到一个扩展 BaseExpandableListAdapter 。在这里,我实现了 getChildView 的方法来设置和返回查看每个子:

I've got and ExpandableList which I assign to an extended BaseExpandableListAdapter. In here, I implement the getChildView method to setup and return a View for each child:

public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View convertView, ViewGroup parent) {

    LinearLayout layout;
    if (convertView == null) {
            layout = (LinearLayout) LayoutInflater.from(MyApp.getContext()).inflate(R.layout.rooms_room_list_row, parent, false);
        } else {
            layout = (LinearLayout) convertView; 
        }

    // Do some custom stuff with views on the layout
    ...

    return layout;

}

调试时,我注意到 getChildView 每个孩子执行两次。所有的(即对groupIndex,childIndex,isLastChild)传递的值是相同的......因此,在第3组,如果我有两个孩子,我会看到:

While debugging, I've noticed that getChildView is executed twice per child. All the values passed in (i.e, groupIndex, childIndex, isLastChild) are the same... so in group 3, if I've got two children, I'll see:

对groupIndex = 3 childIndex = 0

然后

对groupIndex = 3 childIndex = 1

然后重复:

对groupIndex = 3 childIndex = 0

最后:

对groupIndex = 3 childIndex = 1

我的看法显示正常,也就是说,只有两家上市的一群孩子,但为什么它做所有的额外的工作?

My view appears fine, i.e, there are only two children listed for the group, but why is it doing all the extra work?

更新和放大器;答案

看来,如果该列表视图设置为 WRAP_CONTENT 的高度则 getChildView 将被调用两次每名儿童。改变高度 FILL_PARENT 似乎解决这​​个问题。

It seems that if the the listview is set to a height of wrap_content then the getChildView will be called twice per child. Changing the height to fill_parent seems to fix this behavior.

推荐答案

正如我在您的其他问题所提到的,的ListView 电话 getView() 获得的尺寸(宽度和高度)的部分的项目首先,然后的调用 getView()再次实际呈现的项目。检查这个视频输出,它的必读的机器人。与你的问题涉及该位为分41:30。

As I mentioned in your other question, the ListView calls getView() to get the dimensions (width and height) of some items first, then calls the getView() again to actually render the items. Check this video out, it's "required reading" for Android. The bit that deals with your question is at minute 41:30.

这篇关于ExpandableList getChildView每个孩子跑两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆