版权声明:本文为博主原创文章,未经博主允许不得转载。
微博:厉圣杰
文中如有纰漏,欢迎大家留言指出。
使用ListView和GridView时,当列表为空时,默认是不显示任何内容的,这样对用户非常不友好,这时我们就需要通过setEmptyView(View emptyView)来设置当列表为空时的提示。
emptyView 简单使用 Demo
下面展示emptyView使用的简单Demo。
activity_empty_list_view.xml:
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">
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Empty" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="EmptyView可以是ViewGroup" />
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 generateString(int num) {
List list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add("item " + i);
}
return list;
}
}
运行结果如下:
![屏幕快照 2016-09-10 上午9.16.02](http://odsdowehg.bkt.clouddn.com/屏幕快照 2016-09-10 上午9.16.02.png)
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:
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">
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
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:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="假装我是复杂的emptyView" />
运行 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);
}
}