gpt4 book ai didi

java - 空 View 不显示任何内容

转载 作者:行者123 更新时间:2023-12-02 12:24:20 25 4
gpt4 key购买 nike

基本上我有一个ListView(lv),它显示一堆项目,如果lv为空则显示EmptyView,但它什么也不显示代码:

ListView l = (ListView) view.findViewById(R.id.ArrayList);
l.setAdapter(adapter);
srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView);
srl.setOnRefreshListener(srfListener());
View Empty = inflater.inflate(R.layout.empty,null);
l.setEmptyView(Empty);

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:text="Nothing Available "
android:textSize="30sp"
android:textColor="@color/colorPrimary"
android:layout_height="match_parent"
>
</TextView>

有什么想法可以解决这个问题吗?顺便说一句,它不是一个列表 Activity 。

最佳答案

根据您的代码,您正在创建 View 并在列表中添加为空 View ,

View Empty = inflater.inflate(R.layout.empty,null);
l.setEmptyView(Empty);

但是您可以在下面看到实际的代码,它只是使可见并消失,它没有添加到您的布局中。

private void updateEmptyStatus(boolean empty) {
if (isInFilterMode()) {
empty = false;
}

if (empty) {
if (mEmptyView != null) {
mEmptyView.setVisibility(View.VISIBLE);
setVisibility(View.GONE);
} else {
// If the caller just removed our empty view, make sure the list view is visible
setVisibility(View.VISIBLE);
}

// We are now GONE, so pending layouts will not be dispatched.
// Force one here to make sure that the state of the list matches
// the state of the adapter.
if (mDataChanged) {
this.onLayout(false, mLeft, mTop, mRight, mBottom);
}
} else {
if (mEmptyView != null) mEmptyView.setVisibility(View.GONE);
setVisibility(View.VISIBLE);
}
}

因此将其添加到您的布局中,然后将该 View 提供给 setEmptyView

你可以这样添加,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@color/dark_bg_color"
android:gravity="top"
android:orientation="vertical" >
<ListView
android:id="@+id/recentcall_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@color/transparent"
android:divider="#cecece"
android:dividerHeight="1dp"
android:listSelector="@color/transparent"
/>

<ViewStub
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout="@layout/[your empty layout]" />

</LinearLayout>

在你的java代码中,你可以这样调用,

ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty);
l.setEmptyView(emptyViewStub);

关于java - 空 View 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45556058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com