基本上我有一个显示一堆项目的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>
有什么想法解决这个问题?顺便说一下,它不是列表活动。
答案 0 :(得分:1)
根据您的代码,您正在创建视图并在列表中添加为emptyView,
View Empty = inflater.inflate(R.layout.empty,null);
l.setEmptyView(Empty);
但是你可以看到它下方的实际代码只是VISIBLE和GONE,它不会添加到你的布局中。
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>
在你的java代码中,你可以这样调用,
ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty);
l.setEmptyView(emptyViewStub);