ExpandableList getChildView每个子项运行两次

bug*_*ixr 4 android

我有ExpandableList,我分配给扩展BaseExpandableListAdapter.在这里,我实现了getChildView为每个子设置并返回View 的方法:

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;

}
Run Code Online (Sandbox Code Playgroud)

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

groupIndex = 3, childIndex = 0

然后

groupIndex = 3, childIndex = 1

然后它重复:

groupIndex = 3, childIndex = 0

最后:

groupIndex = 3, childIndex = 1

我的观点看起来很好,即该小组只列出了两个孩子,但为什么要做所有额外的工作呢?

更新和答案

似乎如果列表视图设置为高度,wrap_contentgetChildView每个孩子将调用两次.更改高度fill_parent似乎可以解决此问题.

dmo*_*mon 13

正如我在你的另一个问题中所提到的那样,ListView调用getView()首先获取某些项的尺寸(宽度和高度),然后getView()再次调用实际渲染项.查看该视频输出,它的"必读"的Android.处理你问题的那一点是在41:30分钟.

  • 视频说明listview的高度应该是`fill_parent`而不是`wrap_content`.试试(如果是这样的话). (2认同)