android - 可扩展列表让 ChildView 每个 child 运行两次

标签 android

我有 ExpandableList,我将其分配给扩展的 BaseExpandableListAdapter。在这里,我实现了 getChildView 方法来为每个 child 设置和返回一个 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;

}

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

groupIndex = 3, childIndex = 0

然后

groupIndex = 3, childIndex = 1

然后它重复:

groupIndex = 3, childIndex = 0

最后:

groupIndex = 3, childIndex = 1

我的 View 看起来不错,即该组只列出了两个子级,但为什么它要完成所有额外的工作?

更新和回答

似乎如果 ListView 设置为 wrap_content 的高度,那么每个子项将调用两次 getChildView。将高度更改为 fill_parent 似乎可以解决此问题。

最佳答案

正如我在您的其他问题中提到的,ListView 调用 getView() 以获取某些 项的尺寸(宽度和高度)首先,然后 再次调用 getView() 以实际呈现项目。检查this video out ,它是 Android 的“必读”。处理您的问题的时间是 41:30 分。

关于android - 可扩展列表让 ChildView 每个 child 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6557240/

相关文章:

java - Executor 和 PriorityBlockingQueue 上的 ASyncTask

Android源代码编译问题

Android - 在横向和纵向模式之间切换使 Intent 失去值(value)

android - Gradlew Android Studio NoClassDefFoundError:org/gradle/cli/CommandLineParser

android - 使用 vue.js 和 android 的移动应用程序的区别

java - Android 消息/处理程序机制 - 安全吗?

android - 带有页脚和地址栏的 Chrome Android 高度/滚动问题

android - 无法在 SearchView 中自动显示键盘

java - 无法访问 Cloud Firestore 子项的值

android DialogFragment onKeyUp onKeyDown 来自 onKey