• ListView之EmptyView


    From:http://blog.csdn.net/xiangqiao123/article/details/17994099

    继承ListActivity比较方便

    最新开发一个应用程序,需要用到当ListView为空时设置一些View来显示提示内容。我们已经知道ListView有一个公开的方法:setEmptyView(View v)

    可是这个方法的设置是有限制的,就是设置View必需在当前的View hierarchy里,亦即这个View需要被add到当前一个View Tree的一个结点上,如果没有添加到结点上的话,调用setEmptyView(View v)是没有任何效果的。

    它的过程大概是:

    ListView listview = (ListView) findViewById(R.id.list);
    View emptyView = findViewById(R.id.empty);
    ViewGroup parentView = (ViewGroup) listview.getParent();
    parentView.addView(newEmptyView, 2); // 你需要在这儿设置正确的位置,以达到你需要的效果。可能还需要正确的LayoutParamater参数
    listview.setEmptyView(emptyView);

    另:如果你直接设置在XML中也就不需要额外的添加到View Tree中了,因为它已经在那儿了,比如你的Layout是

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" >
        <include layout="@layout/fixed_headerview" />
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"
            android:fastScrollEnabled="true"
            android:textSize="18sp" />
        <TextView
            android:id="@+/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dip"
            android:text="@string/text_no_song"
            android:textSize="22sp"
            android:visibility="gone" />
    </LinearLayout>


    那你只需要以下的代码就可以了:

    ListView listview = (ListView) findViewById(R.id.list);
    View emptyView = findViewById(R.id.empty);
    listview.setEmptyView(emptyView);
  • 相关阅读:
    html5_css 3 学习指南__转
    MySQL常见故障处理手册_转
    MYSQL出错代码列表——转
    Redhat 6环境下安装Oracle 12c的方法
    Wireshark入门:分析DHCP协议的运行
    重命名Oracle数据库的表空间(Renaming a Tablespace)
    Oracle DB 分区特性概述 Overview of Partitions
    Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)
    Supporting Connected Routes to Subnet Zero
    Secondary IP Addressing
  • 原文地址:https://www.cnblogs.com/niray/p/4251336.html
Copyright © 2020-2023  润新知