android - 如何在 ListView 为空时显示标题

标签 android android-layout android-listview android-ui

我正在开发以下屏幕

enter image description here

四个相邻的框是按钮。单击按钮后,我将更改 ListView 的适配器。

由于 ListView 上方的内容占用了大量空间,所以我将整个内容作为标题并通过代码添加。

   myPollsList = (ListView) findViewById(R.id.listArea);
myPollsList.addHeaderView(getLayoutInflater().inflate(R.layout.profile_listview_header, null));

现在我想在列表为空时显示一些 View 。但如果我这样做,那么 header 也会消失。

我在 Activity 而不是 ListActivity 中使用它。即使列表为空也显示标题的任何建议或解决方法?

编辑:请引用我之前的问题 ListView not getting space to show content on smaller screens .这是有人建议我通过将其作为标题来解决问题的地方

最佳答案

我找到了解决这个问题的简单方法。如果没有列表元素并且您没有添加适配器,只需添加:

mListView.setAdapter(null);

然后会出现标题。这比向列表中添加空的/假的项目更容易。

关于android - 如何在 ListView 为空时显示标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18127132/

相关文章:

android - Jetpack Compose 离散 slider 未显示确切的数字

Android - 何时调用 performTraversals?

android - 具有圆形轮廓 ImageView 的滚动 Activity

java - 我正在制作一个双 Webview,它有问题请帮助我

android - 更新 android ListView 中的行内容

android - 如何更改首选项屏幕中的开关按钮颜色?

android - 在哪里/如何查找/提取官方 Android 操作系统(开源)翻译/本地化/术语表字符串

android - 使用两行文本创建一个自定义按钮,每行具有不同的字体

java - 将 Android ListView 行高设置为最大行

android - android 中的 ScrollView 与 ListView