android listview setemptyview,ListView上的setEmptyView在Android应用程序中未显示其视图?...

ListView上的setEmptyView在Android应用程序中未显示其视图?

我从ListView的setEmptyView方法遇到问题。

这是我的Java代码:

ListView view = (ListView)findViewById(R.id.listView1);

view.setEmptyView(findViewById(R.layout.empty_list_item));

ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_list_item_1,

android.R.id.text1, MainController.getInstanz().getItems());

view.setAdapter(adapter1);

empty_list_item:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="@string/emptyList" >

列表显示:

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

我的代码有什么问题?当我有项目时,我会在列表中看到它们。 但是,当列表为空时,我看不到TextView。

gurehbgui asked 2020-07-14T13:22:52Z

12个解决方案

101 votes

您的TextView应该放置在ListView项的正下方,其可见性设置为(android:visibility =“ gone”),请勿将其放置在其他布局中。这是您的主要布局的样子

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/listViewFangbuch"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/empty_list_item"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:visibility="gone"

android:text="@string/emptyList" >

这就是您的代码的样子

ListView view = (ListView)findViewById(R.id.listViewFangbuch);

view.setEmptyView(findViewById(R.id.empty_list_item));

ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_list_item_1,

android.R.id.text1, MainController.getInstanz().getItems());

view.setAdapter(adapter1);

mmbrian answered 2020-07-14T13:23:05Z

32 votes

问题是,我必须做一个addContentView:

View empty = getLayoutInflater().inflate(R.layout.empty_list_item, null, false);

addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

view.setEmptyView(empty);

现在很好

gurehbgui answered 2020-07-14T13:23:30Z

30 votes

对于我来说,这些答案都没有对我有用。 我要做的是手动将空视图(已充气)添加到Listview的“父”视图中:

ListView my_list = (ListView) findViewById(R.id.my_list);

View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);

((ViewGroup)my_list.getParent()).addView(emptyView);

listView.setEmptyView(emptyView);

danigonlinea answered 2020-07-14T13:23:50Z

18 votes

使用setEmptyView()的最简单方法是将“空” TextView和ListView置于相同的布局中,如下所示:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/empty_list_item"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/emptyList"

android:visibility="gone" />

现在您可以使用:

view.setEmptyView(findViewById(R.id.empty_list_item));

Sam answered 2020-07-14T13:24:14Z

13 votes

在尝试了这里介绍的所有解决方案并进行了试验之后,我相信当您的ListView和空View在设置了自动ID的布局中并排放置时,以下代码是最佳的使用方法。

ViewGroup parentGroup = (ViewGroup)getListView().getParent();

View empty = getActivity().getLayoutInflater().inflate(R.layout.list_empty_view,

parentGroup,

false);

parentGroup.addView(empty);

getListView().setEmptyView(empty);

推理:

通过将父组传递给inflate,将尊重空视图布局的layout_ *属性

通过不将视图附加为inflate(false参数),将返回空视图。 否则,inflate会返回父对象,要求我们使用parentGroup.findViewById(empty_view_id)来获取用于setEmptyView()的空视图。 在这里,我们避免了额外的查找,不需要另一个id以及在我们的代码中公开该id的需要。 如果我们不需要将引用保留为空,则告诉inflate附加是正确的操作,从而无需addView调用。

反模式addContentView()不是正确的方法。 只要您的ListView占据了Activity的所有可见空间,它就会等效。 空视图不是ListView的同级对象,而是最终成为Activity中其他根级布局的同级对象。 它之所以看起来有效,是因为它在活动中所有其他视图的顶部浮动(z顺序)。 请参阅:Activity.addContentView(View)== ViewGroup.addContentView(View)?

Harvey answered 2020-07-14T13:24:53Z

6 votes

问题的核心是AdapterView类实际上并未将您传递给setEmptyView()的视图添加到任何容器中。 它只是改变可见性。 如其他答案所述,有很多方法可以将“空视图”对象放入容器中。

AdapterView的文档实际上应该进行更新以反映这一点,因为它并不明显。

Dave answered 2020-07-14T13:25:19Z

4 votes

用XML创建一个布局,该布局同时指定ListView和要使用的空视图。 如果您给他们提供ID @android:id/list和@android:id/empty,则当列表为空时,android会自动将视图与id:empty一起使用。

Nima G answered 2020-07-14T13:25:39Z

3 votes

有一些正确的解决方案,但是我认为这是最好的方法!

View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);

addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView

listView.setEmptyView(emptyView);

listView.setAdapter(adapter);

cesards answered 2020-07-14T13:26:00Z

1 votes

这两种方法也帮助我显示了空视图:

adapter.notifyDataSetChanged();

list.invalidateViews();

user2162746 answered 2020-07-14T13:26:20Z

1 votes

随便用

View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);

((ViewGroup)listView.getParent()).addView(emptyView);

代替listView.setEmptyView(emptyView);

Sazzad Hissain Khan answered 2020-07-14T13:26:45Z

0 votes

我的问题是,我添加了一个页脚视图,它似乎不适用于空视图。删除页脚视图后,将显示空白视图。

Stefan answered 2020-07-14T13:27:05Z

0 votes

我刚学过android,但是摘录自[https://developer.android.com/guide/topics/ui/layout/listview.html]描述了一些不同的方法:

// Create a progress bar to display while the list loads

ProgressBar progressBar = new ProgressBar(this);

progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, Gravity.CENTER));

progressBar.setIndeterminate(true);

getListView().setEmptyView(progressBar);

// Must add the progress bar to the root of the layout

ViewGroup root = (ViewGroup) findViewById(android.R.id.content);

root.addView(progressBar);

ViewGroup根=(ViewGroup)findViewById(android.R.id.content);

Yurii Solopov answered 2020-07-14T13:27:30Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值