最后设置子视图可见性的位置
我正在使用 Android APi 3。 我构建了一个容器控件,其中包含两个控件:另一个容器(布局)和一个 ListView。
在主容器控件中,我重写了 onLayout 函数,以根据列表控件(第二个子视图)中的项目将子视图的可见性更改为 GONE / VISIBLE。
问题是,当我使用 GONE,然后触发事件并将其设置为 VISIBLE 时,第一个子控件显示,但 ListView 不显示该项目 - ListView 显然有一个项目。
也许onLayout可能不是最后一个调用子控件setVisibility的地方。如果没有,最好的地方是什么?
对于我的问题,使用 INVISIBLE 修复问题而不是 GONE。
下面是代码: @覆盖 protected void onLayout(booleanchanged, 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);
}
有什么想法吗?
I am using Android APi 3.
I built a container control that contains two controls: another container (layout) and a ListView.
In the main container control, I overrode onLayout function to change visibility of a child view to GONE / VISIBLE depending on items in the list control (2nd child view).
Problem is, when I use GONE and then an event triggers and set it to VISIBLE, the first child control shows but then ListView doesn't show the item - ListView clearly has one item.
Perhaps onLayout may not be the last place to call setVisibility of child controls. If not, what is the best place?
For my problem, using INVISIBLE fixes problem instead of GONE.
below is the code:
@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);
}
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您需要使用
yourAdapter.notifyDataSetChanged(); 刷新整个 listView;
你这样做了吗……?
probably you need to refresh the whole listView using
yourAdapter.notifyDataSetChanged();
did you do that...?