在 ListView 中隐藏页脚
当我的 Activity 加载时,我会膨胀一个用作页脚的布局文件。我将其附加到 ListView (addFooterView),然后将其可见性设置为 View.GONE。我维护对它的引用,当我希望用户看到它时,我将可见性设置为 View.VISIBLE。
在大多数情况下,这非常有效。然而,页脚似乎仍然占用空间。如果用户使用滚轮/键盘,页脚占用的区域会突出显示。我想进一步完善这一点,使页脚完全消失;理想情况下,无需将其与 ListView 分离。
这可能吗?或者我是否必须设置/取消设置脚而不是简单地切换其可见性?
When my Activity loads, I inflate a layout file that I use for a footer. I attach it to the ListView (addFooterView) and then set its visibility to View.GONE. I maintain a reference to it, and when I want the user to see it, I set the visibility to View.VISIBLE.
For the most part, this works great. However, the footer seems to still take up space. If the user uses the scroll wheel/pad, the area the footer is taking up gets highlighted. I'd like to polish this more so the footer is completely gone; ideally without detaching it from the ListView.
Is this possible? Or am I going to have to set/unset the foot instead of simply toggling its visibility?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
listView.removeFooterView(view)
。最简单的方法是创建一个实例变量来保存膨胀的页脚视图(因此您只需在onCreate()
中膨胀它)。然后只需根据需要调用listView.addFooterView(instanceFooter)
和listView.removeFooterView(instanceFooter)
即可。编辑:
为了让它发挥作用,我正在做以下事情:
onCreate
onResume 中膨胀页脚布局addFooterView( )
然后初始化您的适配器(保留对其的实例引用)并调用setAdapter()
。这将使ListView
“准备好”notifyDatasetChanged()
removeFooterView ()
(如果正在显示,它将隐藏它,否则不执行任何操作)addFooterView()
You can use
listView.removeFooterView(view)
. The easiest way to do this is to create an instance variable to hold your inflated footer view (so you only inflate it inonCreate()
). Then just calllistView.addFooterView(instanceFooter)
andlistView.removeFooterView(instanceFooter)
as needed.Edit:
Here's what I'm doing to get this to work:
onCreate
addFooterView()
THEN initialize your adapter (keep an instance reference to it) and callsetAdapter()
. This will leave theListView
"prepped"notifyDatasetChanged()
removeFooterView()
(it will hide it if it's being displayed and do nothing otherwise)addFooterView()
if the footer needs to be displayed您可以切换可见性。为此,您需要使用线性布局包裹页脚的内容,然后将线性布局可见性设置为 GONE。
在下面的示例中,我将 LogoLinearLayout 的可见性设置为 GONE 并且它起作用了。
You can toggle the visibility. To do that, you need to wrap the content of your footer using a linearlayout, then you set the linearlayout visibility to GONE.
In the example bellow I set the visibility of LogoLinearLayout to GONE and it worked.
调用 addFooterView 时将 isSelectable 参数设置为 false 以禁用页脚选择和突出显示
Set isSelectable parameter to false when You call addFooterView to disable footer selection and highlighting
尝试使用 View.INVISIBLE 而不是 View.GONE。 (我没有尝试过,但它可能会起作用)
Try using View.INVISIBLE instead of View.GONE. (I have not tried this,but it might work)