Android列表ListView控件的使用
在Android开发中,ListView是比较常用的控件,它以列表的形式显示具体内容,并且能够根据数据的长度自适应显示。 在ListView中可以根据需要显示自定义的列表内容,包括文字(TextView)、图片(ImageView)、按钮(Button)等,以此构成图文并茂的显示效果。
在本篇博文中,将使用ListView控件实现简单的博客园首页博文条目显示效果,如图1所示。
图1 ListView实例效果图
1.界面布局
由图1的ListView实例效果图可以看出,在该ListView控件中,我们添加了四条列表项,而每一个列表项都是由五部分构成的,分别是:博文题目(TextView)、作者头像(ImageView)、博文概要(TextView)、作者昵称(TextView)和发布日期(TextView)。
我们需要自己定义一个xml布局文件,来对ListView控件的列表项进行布局,以符合我们的式样需求。这里,我定义了一个名为“listviewitem”的xml布局文件,用来对列表项中的五个控件进行布局,其具体内容如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:descendantFocusability="afterDescendants" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" > 7 8 <!-- 博文题目 --> 9 <TextView 10 android:id="@+id/title" 11 android:layout_marginLeft="5dp" 12 android:layout_marginTop="5dp" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:textStyle="bold" 16 android:textColor="#0000FF" 17 android:lines="1" > 18 </TextView> 19 20 <!-- 作者头像和博文概要 --> 21 <LinearLayout 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:layout_marginTop="2dp" 25 android:layout_marginLeft="5dp" 26 android:orientation="horizontal" > 27 <ImageView 28 android:id="@+id/photograph" 29 android:layout_marginTop="2dp" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" ></ImageView> 32 <TextView 33 android:id="@+id/summary" 34 android:layout_marginLeft="2dp" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:lines="3" ></TextView> 38 </LinearLayout> 39 40 <!-- 作者昵称和发布日期 --> 41 <LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:layout_marginLeft="5dp" 45 android:layout_marginTop="2dp" 46 android:layout_marginBottom="2dp" 47 android:orientation="horizontal" > 48 <TextView 49 android:id="@+id/author" 50 android:layout_width="80dp" 51 android:layout_height="wrap_content" 52 android:textColor="#0000FF" ></TextView> 53 <TextView 54 android:id="@+id/publishtime" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" ></TextView> 57 </LinearLayout> 58 59 </LinearLayout>
可以看出,在该布局文件中,使用了LinearLayout的垂直布局方式将每个列表项分成了上中下三部分,上方显示博文题目,中间则使用LinearLayout的水平布局方式显示作者头像和博文概要,下方同样使用LinearLayout的水平布局方式显示作者昵称和发布日期。
2.ListView的xml属性
ListView的xml属性如图2所示。
图2 ListView的xml属性
其中,android:divider[Drawable/Color]用于设置相邻两个列表项之间的分界线式样;android:dividerHeight[dimension]用于设置相邻两个列表项之间的分界线高度;android:entries[array resource]用于将静态数组中的内容直接填充到ListView列表中。
根据以上的ListView的xml属性介绍,我们便可以activity_main.xml布局文件中设置ListView的属性如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/listview" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:divider="@drawable/dividerimage" > 12 </ListView> 13 14 </LinearLayout>
其中,我们设置了相邻两个列表项之间的分界线式样为一根粉色的分界线。
3.数据加载
如何将数据加载到ListView控件的各个列表项中是使用ListView控件的关键。
通过查看ListView的API帮助文档,我们可以找到相关的设置ListView控件的适配器的方法如下:
public void setAdapter (ListAdapter adapter);
不难看出,ListView控件的适配器类型为ListAdapter,而ListAdapter的子类有ArrayAdapter<T>、BaseAdapter、CursorAdapter、HeaderViewListAdapter、SimpleAdapter、ResourceCursorAdapter、SimpleCursorAdapter和WrapperListAdapter,所以我们可以使用SimpleAdapter作为ListView控件的适配器使用。
简单适配器SimpleAdapter具有良好的可扩充性,可方便的将xml中的控件与所要填充的数据匹配起来。具体实现方法如下:
1 /* 2 * Function : onCreate() 3 * Author : 博客园-依旧淡然 4 */ 5 public void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 mListView = (ListView)this.findViewById(R.id.listview); 10 11 //创建简单适配器SimpleAdapter 12 simpleAdapter = new SimpleAdapter(this, this.getItem(), R.layout.listviewitem, 13 new String[] {"itemTitle","itemPhoto", "itemSummary", "itemAuthor", "itemPublishtime"}, 14 new int[] {R.id.title, R.id.photograph, R.id.summary, R.id.author, R.id.publishtime}); 15 16 //加载SimpleAdapter到ListView中 17 mListView.setAdapter(simpleAdapter); 18 }
在如上的代码中,用到了自定义的getItem()方法,该方法的作用是获取所有的列表内容。我们可以使用HashMap来储存一个列表项中的每一条数据(通过键值对的形式),这样每一个HashMap便储存了一个列表项的所有信息,然后再将HashMap添加到ArrayList中进行储存。如此,我们便可以通过使用自定义的getItem()方法来获取所有的列表内容了。具体的实现方法如下:
1 /* 2 * Function : 获取所有的列表内容 3 * Author : 博客园-依旧淡然 4 */ 5 public ArrayList<HashMap<String, Object>> getItem() { 6 ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>(); 7 for (int i = 0; i < ListViewItemData.getItemNum(); i++) { 8 HashMap<String, Object> map = new HashMap<String, Object>(); 9 map.put("itemTitle", ListViewItemData.getTitle(i)); 10 map.put("itemPhoto", ListViewItemData.getPhotoResId(i)); 11 map.put("itemSummary", ListViewItemData.getSummary(i)); 12 map.put("itemAuthor", ListViewItemData.getAuthor(i)); 13 map.put("itemPublishtime", ListViewItemData.getPublishtime(i)); 14 item.add(map); 15 } 16 return item; 17 }
在如上的代码中,ListViewItemData是自定义的一个类,通过静态数组的形式储存了所有列表项中所要显示的文本和图片资源。
4.事件监听
我们可以为ListView控件设置事件监听器OnItemClickListener,用来对用户的点击事件进行监听,并根据用户所选列表项的不同进行与之相对应的响应。我们需要实现OnItemClickListener接口中的抽象方法onItemClick(),如下:
public void onItemClick(AdapterView<?> parent, View view, int position, long id);
其中,参数position表示各个列表项在ListView控件中所处的位置,自上往下从0开始算起。在该实例中,当点击第一个列表项时,将关闭当前的Activity,并打开一个新的Activity,在新的Activity中,我们通过使用WebView控件来载入我的博客首页,如图3所示。
图3 通过WebView控件载入网页
5.待完善点
在该实例中,只能获取整个列表项的焦点,并响应整个列表项的点击事件,如图4所示。
图4 ListView控件获取焦点示例
如何让每个列表项中的各个子项(比如:博文标题、作者)都可以获取焦点,并响应点击事件,将是后续学习和完善的一个方面。
此外,在该实例中,每个列表项中的内容,都是储存在自定义的ListViewItemData类中的。显然这是不符合实际开发需求的,正确的做法应当是通过解析来自网络的JSON(或XML)数据,然后将其填充到ListView控件列表中。所以,如何解析JSON数据也是后续的一个学习方向。
Android列表ListView控件的使用相关推荐
- android 全选功能,Android实现ListView控件的多选和全选功能实例
本文实例讲述了Android实现ListView控件的多选和全选功能.分享给大家供大家参考,具体如下: 主程序代码 MainActivity.Java package yy.test; import ...
- Android使用ListView控件问题
Android使用ListView控件问题: The application has stopped unexpectedly, please try again. 开发环境:android 1.6 ...
- android中ListView控件onItemClick事件中获取listView传递的数据
http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...
- android 自定义listview控件,一个简单又完整的自定义ListView
ListView 一.简单列表 1.在activity_main中添加控件ListView xmlns:tools="http://schemas.android.com/tools&quo ...
- listview控件Android,Android中ListView控件的简单使用
文章引自郭霖<第一行代码> ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据则会滚动出屏幕 使用LIstView控件 1 在布局文件中引入Li ...
- 解决Android的ListView控件滚动时背景变黑
在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android: ...
- Android 自定义ListView控件,滑动删除
1.触摸事件 dispatchTouchEvent 判断是否处理触摸动作 onTouchEvent 处理触摸动作 2.Android对于控制和获取View在屏幕很强大 ListView: pointT ...
- listview控件在php的使用方法,Android_Android编程之控件ListView使用方法,本文实例讲述了Android编程之控 - phpStudy...
Android编程之控件ListView使用方法 本文实例讲述了Android编程之控件ListView使用方法.分享给大家供大家参考.具体分析如下: 控件ListView是一个重要的控件,可以被用作 ...
- java listview控件_ListView普通列表控件的使用
Android中的列表空间非常灵活,可以自定义每一个列表项,实际上每一个列表项就是一个View,在Android定义了3个列表控件:ListView.ExpandableListView和Spinne ...
最新文章
- 地方弱势运营商如何发展宽带业务?
- IIS不能发布asp.net 应用程序
- 技术人员的明天:35岁后我们做什么
- NSDictionary / NSMutableDictionary 及 NSArray / NSmutableArray (实例)
- 使用腾讯OCR,图片转表格
- Ui学习笔记---EasyUI的使用方法,EasyLoader组件使用
- windows server 2008配置之AD域服务器 1
- 计算机网络技术实验,计算机网络技术实验一
- 解决办法:下列软件包有未满足的依赖关系,依赖。。。但是。。。正要被安装
- 携手业内专家赋能AI时代--EpiK铭识协议发起开源知识运动
- 推荐几个很实用的网址
- 解决Hexo博客的Next主题中阅读全文没有auto_excerpt的问题
- exchange java ews_Exchange服务器之使用EWS读取Exchange邮件
- 论文阅读:Contextual Translation Embedding for Visual Relationship Detection and SGG(PAMI2020)
- 背锅侠?程序出现bug是测试/开发工程师水平有限导致的?我只是个搬砖的......
- mybatis分步查询与延迟加载
- ICDE2020|Predictive Task Assignment in Spatial Crowdsourcing: A Data-driven Approach
- 单片机重启或拔电依旧不能下载程序
- 北斗一号卫星系统 及 一种授时系统
- 试了一个获取Android系统权限的工具Kingroot,运气不错,取得root了。