自动实现ListView无数据时显示其他View
来源:互联网 发布:淘宝十大名店是谁创的 编辑:程序博客网 时间:2024/05/16 13:41
今天看的一块布局是这样的:
<!-- 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>
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。
0 0
- 自动实现ListView无数据时显示其他View
- ListView无数据时显示其他View
- Android ListView无数据时显示其他View
- android之listview无数据时显示自定义view
- ListView,GridView,Spinner等 无数据时显示背景view
- ListView无数据时显示页面
- Android listview加载列表时,无数据,显示默认页
- listview没有数据时显示指定的view
- 使用ListView无数据时,使用ListView的setEmptyView(View emptyView)方法设置空白页面
- ListView添加无数据显示的文本
- listview实现数据列表显示
- 当ListView无数据显示时,默认显示空消息提示
- Wordpress 无插件实现自动显示缩略图
- wordpress插件share to view all 实现自动化用户无感知分享到微博之自动分享到微博后显示指定内容
- ListView无数据,显示点击加载等友好界面
- ajaxpro实现无刷新分页数据显示
- ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听.
- ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听.
- CPP
- 【推荐软件】自从用了 f.lux,再也不用担心被高色温渣屏蓝瞎眼了
- Android 拍照获取图片和图库获取图片
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- 自动实现ListView无数据时显示其他View
- 原php168的一个漏洞
- Intent.ACTION_TIME_TICK 广播
- 日拱一卒(八)
- Linux Apache 配置反向代理
- Makefile经典rumen教程
- 黄金分割二分法数值分析回顾
- Oracle EBS:PO 模块设定注意点Tips
- C/C++ sort函数的用法