使用ListView无数据时,使用ListView的setEmptyView(View emptyView)方法设置空白页面

来源:互联网 发布:e盾网络验证破解4.0 编辑:程序博客网 时间:2024/05/16 14:21

实现步骤:

1: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.example.user.listviewtest3.views.MainActivity">

<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />

<TextView
android:id="@+id/custom_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="暂无数据"
android:textSize="30sp"/>

</LinearLayout>

注意:这里的“空白View组件(custom_empty_view)”需要和“ListView组件”在同一个布局中,否则在java代码中会出现“空白View组件(custom_empty_view)“的对象为null;

2: Java代码

defaultEmptyView = (TextView) findViewById(R.id.custom_empty_view);
Log.e("defaultEmptyView","defaultEmptyView = "+defaultEmptyView);
listView3.setEmptyView(defaultEmptyView);


原创粉丝点击