Java 空视图不显示任何内容
基本上我有一个ListView(lv),它显示一组项目,如果lv为空,则显示EmptyView,但不显示任何代码:Java 空视图不显示任何内容,java,android,xml,Java,Android,Xml,基本上我有一个ListView(lv),它显示一组项目,如果lv为空,则显示EmptyView,但不显示任何代码: ListView l = (ListView) view.findViewById(R.id.ArrayList); l.setAdapter(adapter); srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView); srl.setOnRefreshListener(srfListen
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>
有没有办法解决这个问题??顺便说一下,这不是列表活动。根据您的代码,您正在创建视图并将其添加为列表中的emptyView
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);
}
}
因此,将其添加到布局中,然后将该视图提供给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>
ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty);
l.setEmptyView(emptyViewStub);
您能为您的listview发布XML吗?