看代码学知识之(2) ListView无数据时显示其他View
看代码学知识之(2) ListView无数据时显示其他View
今天看的一块布局是这样的:
<!--
The frame layout is here since we will be showing either
the empty view or the list view.
--> <FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" > <!--
Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it
--> <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" /> <!-- Here is the view to show if the list is emtpy --> <TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No items."
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
这部分布局实际显示的效果却只有一个ListView,里面是数组的数据。
于是,我很好奇这个TextView去了哪里,既然放在同一个FrameLayout中,它难道不是在上层挡着?
显然,唯一没见过的就是这个TextView的id的属性设置:
android:id="@android:id/empty"
搜索了一下,原来这个属性值的作用就是,当ListView关联的Adapter中数据为空时,就显示这个TextView。
而这个ListView中有数据显示时,这个TextView是不可见的。
使用场景1
上面的布局是一个ListFragment所用的布局中的一块,同理,当Activity继承ListActivity时,可以直接实现。
即,在布局中直接用id表达,不需要附加代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- Here is the view to show if the list is emtpy --> <TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No items." /> </FrameLayout>
package com.example.emptylist; import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends ListActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, generateStrings()); setListAdapter(adapter); } private String[] generateStrings() {
String[] strings = new String[0]; for (int i = 0; i < strings.length; ++i) {
strings[i] = "String " + i;
} return strings;
} }
这样当数组长度为0时,将自动显示TextView中的内容。而有数据时,显示ListView。
使用场景2
如果选择不继承ListActivity,则上面例子中的TextView,即便id被设置为android:id="@android:id/empty",也不是只有ListView为空时才显示。
事实上,因为在FrameLayout中,所以这个TextView会一直显示在ListView层之上。
当ListView无数据时只显示TextView;但是ListView有数据时,仍然显示这个提示“No items”的TextView(which is obviously wrong!)。
此时这个TextView的显示与否和ListView的数据没有什么关系了。
对于不继承ListActivity的情况,要实现上面的效果应该如何呢?
首先,ListView和TextView的id可以任意设置。
然后,只需要调用在代码中调用setEmptyView(View emptyView)设置ListView为空时显示这个TextView即可。
布局和代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- Here is the view to show if the list is emtpy --> <TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No items." /> </FrameLayout>
Activity:
package com.example.emptylist; import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private ListView mListView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.myList);
mListView.setEmptyView(findViewById(R.id.myText)); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, generateStrings()); mListView.setAdapter(adapter); } private String[] generateStrings() {
String[] strings = new String[100]; for (int i = 0; i < strings.length; ++i) {
strings[i] = "String " + i;
} return strings;
} }
扩展:
因为setEmptyView(View emptyView)这个方法是属于AdapterView这个类的,所以除了ListView之外,其他的子类,如GridView,Spinner等,应该也可以用这个方法来设置Adapter数据为空时显示另一个View。
看代码学知识之(2) ListView无数据时显示其他View的更多相关文章
- 剥开比原看代码13:比原是如何通过/list-balances显示帐户余额的?
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- 剥开比原看代码16:比原是如何通过/list-transactions显示交易信息的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- GridControl 无数据时显示信息
图例: 主要代码如下: 说明:给GridView添加事件gv_CustomDrawEmptyForeground private void gv_CustomDrawEmptyForeground(o ...
- Android ListView无数据项时提示
只需要调用在代码中调用setEmptyView(View emptyView)设置ListView为空时显示这个TextView即可. 布局文件 <?xml version="1.0& ...
- 跟vczh看实例学编译原理——一:Tinymoe的设计哲学
自从<序>胡扯了快一个月之后,终于迎来了正片.之所以系列文章叫<看实例学编译原理>,是因为整个系列会通过带大家一步一步实现Tinymoe的过程,来介绍编译原理的一些知识点. 但 ...
- 跟vczh看实例学编译原理——零:序言
在<如何设计一门语言>里面,我讲了一些语言方面的东西,还有痛快的喷了一些XX粉什么的.不过单纯讲这个也是很无聊的,所以我开了这个<跟vczh看实例学编译原理>系列,意在科普一些 ...
- java开发必学知识:动态代理
目录 1. 引言 2. 代理模式及静态代理 2.1 代理模式说明 2.2 静态代理 2.3 静态代理局限性 3. 动态代理 3.1 JAVA反射机制 3.2 JDK动态代理 3.2.1 JDK动态代理 ...
- 跟vczh看实例学编译原理——三:Tinymoe与无歧义语法分析
文章中引用的代码均来自https://github.com/vczh/tinymoe. 看了前面的三篇文章,大家应该基本对Tinymoe的代码有一个初步的感觉了.在正确分析"print ...
- 看日记学git摘要~灰常用心的教程
看日记学git linux 命令行 cd ls / ls -a clear mkdir rmdir echo "hi, good day" > hi.txt touch he ...
随机推荐
- Microsoft Azure News(4) Azure新D系列虚拟机上线
<Windows Azure Platform 系列文章目录> Update 2016-05-07 注意事项: Azure的数据中心建设是有先后顺序的,最早是落地了A系列的虚拟机,然后是D ...
- 深度学习中的Data Augmentation方法(转)基于keras
在深度学习中,当数据量不够大时候,常常采用下面4中方法: 1. 人工增加训练集的大小. 通过平移, 翻转, 加噪声等方法从已有数据中创造出一批"新"的数据.也就是Data Augm ...
- 如何做好一个ORM框架
很多人都不太认可以第三方ORM,因为考虑的点不够全面,没有用户群体大的ORM有保证,这点是不可否认确是事实. 但是往往用户群体大的ORM又有不足之处,就拿用户群体最多的两个ORM来说一下吧 1.EF ...
- apache(nginx)+django+virutalenv(virtualenvwrapper)+gunicorn+supervisor配置高效web环境
前言 django的调试模式配置简单,用于测试十分方便,但众所周知,这个只适合于调试,生产上运行效率十分低下. 后来考虑用nginx+uwsgi的模式进行,但之前配置过apache+wsgi的方式,感 ...
- WCF服务创建与使用(请求应答模式)
不说废话,直接上代码.以下服务创建是在独立的WCF类库中,若采用WCF应程程序,定义及创建服务代码均相同,但文件名不同,是CalculatorService.svc 第一步,定义服务契约(Servic ...
- C语言学习019:函数指针
在C语言中,函数名也是指针变量,比如创建了一个add(int n,int m)的函数的同时也创建了一个名为add的指针变量,因此我们可以把函数指针当作一种类型为它赋值.当作参数传递等操作 C语言创建函 ...
- 选择排序---直接选择排序算法(Javascript版)
在要排序的一组数列中,选出最小(或者最大)的一个数与第1个位置的数交换:然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个 ...
- C/C++程序员必须熟练应用的开源项目
作为一个经验丰富的C/C++程序员, 肯定亲手写过各种功能的代码, 比如封装过数据库访问的类, 封装过网络通信的类,封装过日志操作的类, 封装过文件访问的类, 封装过UI界面库等, 也在实际的项目中应 ...
- PHP导入excel数据到MYSQL
这里介绍一个直接将excel文件导入mysql的例子.我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用.PHP-ExcelReader,下载地址: http://sourceforge. ...
- android app自动化测试之UIAutomator
一.UIAutomator Android自动化测试工具有很多,但是要免费.易上手,本人觉得就直接使用Eclipse自带的UIAutomator就不错.测试人员无需跟开发要代码信息,只要手机上有安装之 ...