Android listview加载列表时,无数据,显示默认页

在Android开发中,当ListView加载列表时如果服务器无数据,原本会显示空白页面,这影响用户体验。通过在XML布局中设置一个RefreshListView,并在Fragment或Activity中调用setEmptyView(View emptyView)方法,可以指定在ListView无数据时显示默认提示页面,提升用户体验。此方法同样适用于GridView和Spinner等AdapterView子类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
Activity->2个Fragment
未购买页面,服务器端暂时没有数据,但是显示空白页面,显然不好看,用户体验也不好,所以当没有数据时候显示默认提示页面

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" >

    <LinearLayout style="@style/resum_base_linerlayout" >

        <TextView
            style="@style/resum_base_title"
            android:text="按时间顺序" />

        <View style="@style/resum_base_view" />

        <TextView
            android:id="@+id/releaseTime"
            android:layout_marginRight="30dip"
            style="@style/resum_base_desc"
            android:layout_weight="1"
            android:text="最近三个月"
            android:drawableRight="@drawable/pull_down"></TextView>
    </LinearLayout>

    <com.runsheng.zyzp4a.view.RefreshListView
        android:id="@+id/tPurchase_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="0dip" >
    </com.runsheng.zyzp4a.view.RefreshListView>

 <LinearLayout 
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:gravity="center"
        android:orientation="vertical"
        >
        <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:gravity="center"
         android:src="@drawable/aa"
        />
     <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:gravity="center"
        android:textSize="25sp"
        android:text="SORRY,本页面没有内容!" />
    </LinearLayout>
</LinearLayout>

布局很简单,com.runsheng.zyzp4a.view.RefreshListView就是一个Listview,
在fragment或activity中加上

private RefreshListView lv;
.........
    lv=(RefreshListView) view.findViewById(R.id.tPurchase_lv);
    lv.setEmptyView(view.findViewById(R.id.myText));

RefreshListView==Listview
只需要调用在代码中调用setEmptyView(View emptyView)设置ListView为空时显示这个默认布局即可。

 因为setEmptyView(View emptyView)这个方法是属于AdapterView这个类的,所以除了ListView之外,其他的子类,如GridView,Spinner等,应该也可以用这个方法来设置Adapter数据为空时显示另一个View。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值