关于ListView为空时的显示技巧
来源:互联网 发布:最好的mac软件下载网站 编辑:程序博客网 时间:2024/05/01 07:45
转载自 http://blog.csdn.net/vector_yi/article/details/24936163
在移动平台上为用户展示数据的一个常用方法是将数据填充进一个List内,而此时需要注意的一点就是:
如何处理需要填充的数据为空的情况?
ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。
当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
看看XML布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<
FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" >
<
ListView
android:id ="@+id/my_list_view"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" />
<
ImageView
android:id ="@+id/empty_view"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:src ="@drawable/empty_view" />
</
FrameLayout
>
再来看自定义的drawable/empty_view文件:
1
2
3
4
<
shape
xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<
solid
android:color= "#AA00FF00" />
</
shape
>
是一个自定义的shape,当ListView没数据的时候才展现出来。
最后再看MainActivity文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public
class
MainActivity
extends
Activity {
private
ListView mListView;
@Override
public
void
onCreate (Bundle savedInstanceState ) {
super
. onCreate( savedInstanceState );
setContentView (R .layout .main );
mListView = (ListView ) findViewById (R .id .my_list_view );
mListView. setEmptyView (findViewById (R .id .empty_view ));
/*String[] strs=new String[]{"1","2"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,strs);
mListView.setAdapter(adapter);*/
}
}
仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。
0 0
- 关于ListView为空时的显示技巧
- android listView的为空时显示
- 当ListView为空时,显示特定的View
- listview为空时,显示字符串
- 当Listview为空时显示提示字符,并且不为ListActivity的子类
- listview内容为空的时候显示自定义的内容
- ListView在数据为空的时候headView不显示
- listview为空时的代码
- 当ListView为空时显示提示语
- Android之listview为空时显示字符串
- Android之listview为空时显示字符串
- Android之listview为空时显示布局
- listview设置数据为空时显示数据
- Android ListFragment下ListView为空时如何显示空视图
- “空”的艺术-当数据为空时显示什么
- “空”的艺术-当数据为空时显示什么
- “空”的艺术-当数据为空时显示什么
- “空”的艺术-当数据为空时显示什么
- Redis 数据类型 Strings
- JAXP下的XML解析用法(包括dom解析和sax解析)
- 免费开放网站-书籍电子商务--成绩管理系统
- LeetCode119——Pascal's Triangle II
- Linux平台驱动分析
- 关于ListView为空时的显示技巧
- Python学习笔记(5)-多重继承的坑
- iOS动画进阶(八)显式动画
- linux文件权限学习笔记
- toad for oracle, 格式化配置 备份FmtPlus.opt
- POJ 2923 Relocation(状压+背包)
- iOS动画进阶(九)图层时间
- Android Chromium:成功地获取到用户自定义的系统默认字体设置
- 2015 寒假搜索专题 K - Backward Digit Sums