我正在使用 Android APi 3。我构建了一个包含两个控件的容器控件:另一个容器(布局)和一个 ListView。
在主容器控件中,我重写了 onLayout 函数以根据列表控件(第二个子视图)中的项目将子视图的可见性更改为 GONE / VISIBLE。
问题是,当我使用 GONE 然后触发事件并将其设置为 VISIBLE 时,第一个子控件显示但 ListView 不显示该项目 - ListView 显然有一个项目。
也许 onLayout 可能不是调用子控件的 setVisibility 的最后一个地方。如果没有,最好的地方是什么?
对于我的问题,使用 INVISIBLE 修复问题而不是 GONE。
下面是代码:@Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
/* if there's no data to display, hide headerView */
if (listView.getAdapter() == null || listView.getAdapter().getCount() == 0)
headerView.setVisibility(INVISIBLE); // if set to GONE, it won't display the listView when there's only one item or unless it's refreshed 2nd time.
else
headerView.setVisibility(VISIBLE);
super.onLayout(changed, l, t, r, b);
}
有什么想法吗?