给ListView设置emptyView
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这时我们就需要通过setEmptyView(View emptyView)
来设置当列表为空时的提示。
emptyView简单使用Demo
下面展示emptyView使用的简单Demo。
activity_empty_list_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.littlejie.listview.EmptyListViewActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<!-- ListView的emptyView,默认不需设置其visiblity属性设置为GONE -->
<!-- 当ListView为空时,emptyView会自动被设置为GONE -->
<!-- 当ListView不为空时,emptyView会自动被设置为VIBISIBLE -->
<LinearLayout
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Empty" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="EmptyView可以是ViewGroup" />
</LinearLayout>
</LinearLayout>
EmptyListViewActivity.java
:
public class EmptyListViewActivity extends Activity {
private ListView mLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty_list_view);
mLv = (ListView) findViewById(R.id.lv);
mLv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, generateString(0)));
mLv.setEmptyView(findViewById(android.R.id.empty));
}
private List<String> generateString(int num) {
List<String> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add("item " + i);
}
return list;
}
}
运行结果如下:

emptyView复杂使用Demo
前一个Demo讲了emptyView的简单使用方法,那如果我们要自定义emptyView或者emptyView的布局跟ListView不在同一个布局文件中呢?恩,你可能会说:直接调用setEmptyView(View emptyView)
不行嘛,难道有啥不一样?
看来,你还是too young too simple。直接调用的结果就是:emptyView根本就不会显示!!!
新建一个ComplexEmptyListViewActivity.java
:
public class ComplexEmptyListViewActivity extends Activity {
private ListView mLv;
private EmptyView mEmptyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complex_empty_list_view);
mLv = (ListView) findViewById(R.id.lv);
mLv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, Utils.generateString(0)));
mEmptyView = new EmptyView(this);
//此处是重点,如果不将mEmptyView添加进当前的ViewGroup,mEmptyView都不可见
//((ViewGroup) mLv.getParent()).addView(mEmptyView);
mLv.setEmptyView(mEmptyView);
}
}
布局文件activity_complex_empty_list_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.littlejie.listview.EmptyListViewActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
EmptyView
:
public class EmptyView extends LinearLayout {
public EmptyView(Context context) {
super(context);
init(context);
}
public EmptyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.complex_empty_view, this);
}
}
complex_empty_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="假装我是复杂的emptyView" />
</LinearLayout>
运行demo,你会发现ListView为空时,根本就没显示emptyView。这是为什么呢?因为emptyView根本就没添加到当前Activity的布局中,所以即使设置了emptyView也不会显示,解决方法就是把emptyView添加到当前Activity的布局中(ComplexEmptyListViewActivity注释的内容)。
补充
细心的可能已经发现,在两个demo中,我们都没去设置emptyView的
visiblity
属性,这是为什么呢?其实看下源码就知道了~当调用
setEmptyView(View emptyView)
方法时,ListView会调用到updateEmptyStatus(boolean empty)
,在这里系统会跟empty去判断是否显示emptyView。恩,就这么简单~/** * Sets the view to show if the adapter is empty */ @android.view.RemotableViewMethod public void setEmptyView(View emptyView) { mEmptyView = emptyView; // If not explicitly specified this view is important for accessibility. if (emptyView != null && emptyView.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { emptyView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); } final T adapter = getAdapter(); final boolean empty = ((adapter == null) || adapter.isEmpty()); updateEmptyStatus(empty); } /** * Update the status of the list based on the empty parameter. If empty is true and * we have an empty view, display it. In all the other cases, make sure that the listview * is VISIBLE and that the empty view is GONE (if it's not null). */ 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); } }