Android学习笔记之ListView
jopen
13年前
<p><1>简介</p> <p>在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。</p> <p>列表的显示需要三个元素:</p> <p>1.ListVeiw 用来展示列表的View。</p> <p>2.适配器 用来把数据映射到ListView上的中介。</p> <p>3.数据 具体的将被映射的字符串,图片,或者基本组件。</p> <p>根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter</p> <p>其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。 SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。</p> <p><2>类继承</p> <p>java.lang.Object</p> <p align="left"> <a href="/misc/goto?guid=4959517929565595095">android.view.View</a></p> <p align="left"> <span>android.view.ViewGroup</span></p> <p align="left"><span> android.widget.AdapterView<T extends android.widget.Adapter></span></p> <p align="left"><span> </span> <span>android.widget.AbsListView</span></p> <p align="left"><span> </span> <span>android.widget.ListView</span></p> <p> </p> <p><3>XML属性</p> <p> </p> <table style="border-collapse:collapse;margin-left:26.7pt;" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="border-bottom:black 1pt solid;border-left:black 1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;background:#dee8f1;border-top:black 1pt solid;border-right:black 1pt solid;padding-top:0cm;" valign="top" width="198"> <p align="center"><span style="font-family:宋体;">属性名称</span></p> </td> <td style="border-bottom:black 1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;background:#dee8f1;border-top:black 1pt solid;border-right:black 1pt solid;padding-top:0cm;" valign="top" width="321"> <p align="center"><span style="font-family:宋体;">描述</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:choiceMode</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">规定此</span>ListView<span style="font-family:宋体;">所使用的选择模式。缺省状态下,</span>list<span style="font-family:宋体;">没有选择模式。</span></p> <p align="center"><span style="font-family:宋体;">属性值必须设置为下列常量之一:</span> none<span style="font-family:宋体;">,值为</span>0<span style="font-family:宋体;">,表示无选择模式;</span></p> <p align="center">singleChoice<span style="font-family:宋体;">,值为</span>1<span style="font-family:宋体;">,表示最多可以有一项被选中;</span></p> <p align="center">multipleChoice<span style="font-family:宋体;">,值为</span>2<span style="font-family:宋体;">,表示可以多项被选中。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>choiceMode<span style="font-family:宋体;">。</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:divider</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">规定</span>List<span style="font-family:宋体;">项目之间用某个图形或颜色来分隔。可以用</span>"@[+][package:]type:name"<span style="font-family:宋体;">或者</span>"?[package:][type:]name"<span style="font-family:宋体;">(主题属性)的形式来指向某个已有资源;也可以用</span>"#rgb"<span style="font-family:宋体;">,</span>"#argb"<span style="font-family:宋体;">,</span>"#rrggbb"<span style="font-family:宋体;">或者</span>"#aarrggbb"<span style="font-family:宋体;">的格式来表示某个颜色。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>divider<span style="font-family:宋体;">。</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:dividerHeight</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">分隔符的高度。若没有指明高度,则用此分隔符固有的高度。必须为带单位的浮点数,如</span>"14.5sp"<span style="font-family:宋体;">。可用的单位如</span>px<span style="font-family:宋体;">(</span>pixel<span style="font-family:宋体;">像素),</span>dp<span style="font-family:宋体;">(</span>density-independent pixels <span style="font-family:宋体;">与密集度无关的像素)</span>, sp<span style="font-family:宋体;">(</span><span>scaled pixels based on preferred font size</span><span style="font-family:宋体;">基于字体大小的固定比例的像素),</span> in (inches<span style="font-family:宋体;">英寸</span><span>), mm (millimeters</span><span style="font-family:宋体;">毫米</span>)<span style="font-family:宋体;">。</span></p> <p align="center"><span style="font-family:宋体;">可以用</span>"@[package:]type:name</p> <p align="center">"<span style="font-family:宋体;">或者</span>"?[package:][type:]name"<span style="font-family:宋体;">(主题属性)的格式来指向某个包含此类型值的资源。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>dividerHeight<span style="font-family:宋体;">。</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:entries</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">引用一个将使用在此</span>ListView<span style="font-family:宋体;">里的数组。若数组是固定的,使用此属性将比在程序中写入更为简单。</span></p> <p align="center"><span style="font-family:宋体;">必须以</span>"@[+][package:]type:name"<span style="font-family:宋体;">或者</span> "?[package:][type:]name"<span style="font-family:宋体;">的形式来指向某个资源。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>entries<span style="font-family:宋体;">。</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:footerDividersEnabled</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">设成</span>flase<span style="font-family:宋体;">时,此</span>ListView<span style="font-family:宋体;">将不会在页脚视图前画分隔符。此属性缺省值为</span>true<span style="font-family:宋体;">。</span></p> <p align="center"><span style="font-family:宋体;">属性值必须设置为</span>true<span style="font-family:宋体;">或</span>false<span style="font-family:宋体;">。</span></p> <p align="center"><span style="font-family:宋体;">可以用</span>"@[package:]type:name</p> <p align="center">"<span style="font-family:宋体;">或者</span>"?[package:][type:]name"<span style="font-family:宋体;">(主题属性)的格式来指向某个包含此类型值的资源。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>footerDividersEnabled<span style="font-family:宋体;">。</span></p> </td> </tr> <tr> <td style="border-bottom:1pt solid;border-left:1pt solid;padding-bottom:0cm;padding-left:5.4pt;width:148.8pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="198"> <p align="center"><strong><span>android:headerDividersEnabled</span></strong></p> </td> <td style="border-bottom:1pt solid;border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:241pt;padding-right:5.4pt;border-top:medium none;border-right:1pt solid;padding-top:0cm;" width="321"> <p align="center"><span style="font-family:宋体;">设成</span>flase<span style="font-family:宋体;">时,此</span>ListView<span style="font-family:宋体;">将不会在页眉视图后画分隔符。此属性缺省值为</span>true<span style="font-family:宋体;">。</span></p> <p align="center"><span style="font-family:宋体;">属性值必须设置为</span>true<span style="font-family:宋体;">或</span>false<span style="font-family:宋体;">。</span></p> <p align="center"><span style="font-family:宋体;">可以用</span>"@[package:]type:name</p> <p align="center">"<span style="font-family:宋体;">或者</span>"?[package:][type:]name"<span style="font-family:宋体;">(主题属性)的格式来指向某个包含此类型值的资源。</span></p> <p align="center"><span style="font-family:宋体;">可参看全局属性资源符号</span>headerDividersEnabled<span style="font-family:宋体;">。</span></p> </td> </tr> </tbody> </table> <p><4>公共方法</p> <p align="left"><span> <span style="background:#d9d9d9;">public void addFooterView (View v)</span></span></p> <p align="left"><span> </span><span style="font-family:宋体;">加一个固定显示于</span>list<span style="font-family:宋体;">底部的视图。如果此方法被调用超过一次,所加的几个视图将按照它们加入的顺序排列。加入的视图可取得焦点。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">注意:在调用</span>setAdapter<span style="font-family:宋体;">之前调用此方法。这样的话,可以利用点击光标来收起有</span>header view<span style="font-family:宋体;">和</span>footer view<span style="font-family:宋体;">的</span>ListView<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;margin-left:21pt;" align="left"> <span style="font-family:宋体;">参数</span></p> <p align="left"><span> </span> <span>v </span><span style="font-family:宋体;">要加的视图</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void addFooterView (View v, Object data, boolean isSelectable)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">加一个固定显示于</span>list<span style="font-family:宋体;">底部的视图。如果此方法被调用超过一次,所加的几个视图将按照它们加入的顺序排列。加入的视图可取得焦点。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">注意:在调用</span>setAdapter<span style="font-family:宋体;">之前调用此方法。这样的话,可以利用点击光标来收起有</span>header view<span style="font-family:宋体;">和</span>footer view<span style="font-family:宋体;">的</span>ListView<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;margin-left:21pt;" align="left"> <span style="font-family:宋体;">参数</span></p> <p align="left"><span> </span> <span>v </span><span style="font-family:宋体;">要加的视图</span></p> <p align="left"><span> </span> <span>data </span><span style="font-family:宋体;">和此视图关联的数据</span></p> <p align="left"><span> </span> <span>isSelectable </span><span style="font-family:宋体;">设为</span>true<span style="font-family:宋体;">则表示</span>footer view<span style="font-family:宋体;">可以被选中</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void addHeaderView (View v)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">加一个固定显示于</span>list<span style="font-family:宋体;">顶部的视图。如果此方法被调用超过一次,所加的几个视图将按照它们加入的顺序排列。加入的视图可取得焦点。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">注意:在调用</span>setAdapter<span style="font-family:宋体;">之前调用此方法。这样的话,可以利用点击光标来收起有</span>header view<span style="font-family:宋体;">和</span>footer view<span style="font-family:宋体;">的</span>ListView<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;margin-left:21pt;" align="left"> <span style="font-family:宋体;">参数</span></p> <p align="left"><span> </span> <span>v </span><span style="font-family:宋体;">要加的视图</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void addHeaderView (View v, Object data, boolean isSelectable)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">加一个固定显示于</span>list<span style="font-family:宋体;">顶部的视图。如果此方法被调用超过一次,所加的几个视图将按照它们加入的顺序排列。加入的视图可取得焦点。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">注意:在调用</span>setAdapter<span style="font-family:宋体;">之前调用此方法。这样的话,可以利用点击光标来收起有</span>header view<span style="font-family:宋体;">和</span>footer view<span style="font-family:宋体;">的</span>ListView<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;margin-left:21pt;" align="left"> <span style="font-family:宋体;">参数</span></p> <p align="left"><span> </span> <span>v </span><span style="font-family:宋体;">要加的视图</span></p> <p align="left"><span> </span> <span>data </span><span style="font-family:宋体;">和此视图关联的数据</span></p> <p align="left"><span> </span> <span>isSelectable </span><span style="font-family:宋体;">表示此</span>header view<span style="font-family:宋体;">可选与否</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void clearChoices ()</span></p> <p align="left"><span> </span><span style="font-family:宋体;">取消之前设置的任何选择</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean dispatchKeyEvent (KeyEvent event)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">按照可以获得焦点的顺序(从视图树的顶端到当前获得焦点的视图),分派一个按键事件给下一个视图。若此视图有焦点,事件将会分派给它自己。否则它将按照顺序,分派给下一个节点。此方法同时触动所有按键监听器。</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">参数</span></p> <p align="left"><span> event </span><span style="font-family:宋体;">被分派的事件</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件被处理,则返回</span>true<span style="font-family:宋体;">;否则为</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">在视图的子项目被构建时,分派一个辅助事件。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> event </span><span style="font-family:宋体;">事件</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件全部完成,则返回</span>true</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public ListAdapter getAdapter () </span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span>ListView<span style="font-family:宋体;">当前用的适配器。返回的适配器不可以和传给</span>setAdapter(ListAdapter)<span style="font-family:宋体;">的参数一样,但是可以是</span>WrapperListAdapter<span style="font-family:宋体;">。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">当前用来显示</span>ListView<span style="font-family:宋体;">中数据的适配器</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> setAdapter(ListAdapter)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public long[] getCheckItemIds ()</span> </p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="font-family:宋体;background:yellow;">此方法已经过时了。使用</span><code><span style="background:yellow;font-size:12pt;"><a href="/misc/goto?guid=4959517929642560755"><span style="font-family:'Calibri','sans-serif';font-size:10.5pt;">getCheckedItemIds()</span></a></span></code><span style="font-family:宋体;background:yellow;">代替。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回被选中项目的索引集合。只有当选择模式没有被设置为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">时才有效。</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public long[] getCheckedItemIds ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回被选中项目的索引集合。只有当选择模式没有被设置为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">,并且适配器有稳定的</span> ID<span style="font-family:宋体;">(</span>hasStableIds()==true<span style="font-family:宋体;">)</span><span style="font-family:宋体;">时,结果才有效。</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">一个新的数组,包含列表中每个被选中的索引(</span>id<span style="font-family:宋体;">)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getCheckedItemPosition () </span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回当前被选中的项目。只有当选择模式已被设置为</span>CHOICE_MODE_SINGLE<span style="font-family:宋体;">时</span><span style="font-family:宋体;">,结果才有效。</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">返回当前被选中的项目的索引;若没有项目被选中,则返回</span>INVALID_POSITION</p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> </span> <span>setChoiceMode(int)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public SparseBooleanArray getCheckedItemPositions ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回当前被选中的项目集合。只有当选择模式没有被设置为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">时</span><span style="font-family:宋体;">,结果才有效。</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span> <span style="font-family:宋体;">类型为</span>SparseBooleanArray<span style="font-family:宋体;">的值,其中,对每一个索引所代表的项目,若被选中,则返回</span>true<span style="font-family:宋体;">;当选择模式被设置为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">时</span><span style="font-family:宋体;">,返回</span>null<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getChoiceMode ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回当前的选择模式</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> setChoiceMode(int)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public Drawable getDivider ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回当前画在列表元素之间,作为分隔符的图形</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getDividerHeight ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回分隔符的高度</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getFooterViewsCount () </span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">列表中的页脚视图数量;缺省实现时,数量为</span>0</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getHeaderViewsCount ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">列表中的页眉视图数量;缺省实现时,数量为</span>0</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean getItemsCanFocus ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> ListAdapter</span><span style="font-family:宋体;">所生成的视图是否可以包含能取得焦点的项目</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public int getMaxScrollAmount ()</span> </p> <p align="left"><span> </span> <span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span>The maximum amount a list view will scroll in response to an arrow event.</span></p> <p align="left"><span> </span><span style="font-family:宋体;">响应箭头事件时,列表视图可以滚动的最大值。(译者注:此处翻译待改进,恐怕需要仔细查看源代码才能明白其含义,也可以用</span>Google Code<span style="font-family:宋体;">搜索相关的代码)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean isItemChecked (int position)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">对于由</span>position<span style="font-family:宋体;">指定的项目,返回其是否被选中。只有当选择模式已被设置为</span>CHOICE_MODE_SINGLE<span style="font-family:宋体;">或</span>CHOICE_MODE_MULTIPLE<span style="font-family:宋体;">时</span><span style="font-family:宋体;">,结果才有效。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> position </span><span style="font-family:宋体;">要返回选中状态的项目</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回项目的选中状态;若选择模式无效,则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean onKeyDown (int keyCode, KeyEvent event)</span> </p> <p align="left"><span> KeyEvent.Callback.onKeyMultiple()</span><span style="font-family:宋体;">的缺省实现:若视图被激活并且可以被点击,当出现</span>KEYCODE_DPAD_CENTER<span style="font-family:宋体;">和</span>KEYCODE_ENTER<span style="font-family:宋体;">代表的行为时,做点击该视图的动作。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> keyCode </span><span style="font-family:宋体;">表示按某个按键的按键代号,参见</span>KeyEvent</p> <p align="left"><span> event </span><span style="font-family:宋体;">定义按键动作的按键事件对象</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件被成功处理,则返回</span>true<span style="font-family:宋体;">;若想要下一个接收器处理该事件,则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent event)</span></p> <p align="left"><span> KeyEvent.Callback.onKeyMultiple()</span><span style="font-family:宋体;">的缺省实现:总是返回</span>false<span style="font-family:宋体;">(不处理该事件)。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> keyCode </span><span style="font-family:宋体;">表示按某个按键的按键代号,参见</span>KeyEvent</p> <p align="left"><span> repeatedCount </span><span style="font-family:宋体;">实现动作的次数</span></p> <p align="left"><span> event </span><span style="font-family:宋体;">定义按键动作的按键事件对象</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件被成功处理,则返回</span>true<span style="font-family:宋体;">;若想要下一个接收器处理该事件,则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean onKeyUp (int keyCode, KeyEvent event)</span> </p> <p align="left"><span> KeyEvent.Callback.onKeyMultiple()</span><span style="font-family:宋体;">的缺省实现:当出现</span>KEYCODE_DPAD_CENTER<span style="font-family:宋体;">和</span>KEYCODE_ENTER<span style="font-family:宋体;">代表的行为时,做点击该视图的动作。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> keyCode </span><span style="font-family:宋体;">表示按某个按键的按键代号,参见</span>KeyEvent</p> <p align="left"><span> event </span><span style="font-family:宋体;">定义按键动作的按键事件对象</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件被成功处理,则返回</span>true<span style="font-family:宋体;">;若想要下一个接收器处理该事件,则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void onRestoreInstanceState (Parcelable state) </span></p> <p align="left"><span> </span><span style="font-family:宋体;">重新创建并显示一个视图,此视图拥有之前</span>onSaveInstanceState()<span style="font-family:宋体;">保存的内部状态。当</span>state<span style="font-family:宋体;">为</span>null<span style="font-family:宋体;">时,此方法不会被调用。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> state </span><span style="font-family:宋体;">之前</span>onSaveInstanceState()<span style="font-family:宋体;">保存的状态</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public Parcelable onSaveInstanceState ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">保存视图的内部状态,用于以后创建新的拥有同样状态的实例。可保存的状态只包含非持久性的,或者可重新组建的信息。比如,永远不可能保存你当前在屏幕上的位置,因为当新的实例被放置于视图层次体系中时,位置会被重新计算。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">一些可以被保存的状态:文本视图(但是通常不是指文本本身,因为文本是被保存在内容提供商或其他持久性的储存体中)中当前的光标位置;列表视图中当前的选中项。</span></p> <p style="text-align:left;text-indent:21pt;margin-left:21pt;" align="left"> <span style="font-family:宋体;">返回</span><span> </span></p> <p style="text-align:left;text-indent:21pt;margin-left:42pt;" align="left"> <span style="font-family:宋体;">返回一个包含视图当前动态状态的接口方法对象;若没有东西被保存,则返回</span>null<span style="font-family:宋体;">。缺省情况下返回</span>null<span style="font-family:宋体;">。</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean onTouchEvent (MotionEvent ev)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">此方法用于处理触摸屏的动作事件。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> ev </span><span style="font-family:宋体;">动作事件</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若事件被成功处理,则返回</span>true<span style="font-family:宋体;">;否则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean performItemClick (View view, int position, long id) </span></p> <p align="left"><span> </span><span style="font-family:宋体;">调用定义好的</span>OnItemClickListener<span style="font-family:宋体;">。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> view AdapterView</span><span style="font-family:宋体;">中被点击到的视图</span></p> <p align="left"><span> position </span><span style="font-family:宋体;">视图在适配器中的索引</span></p> <p align="left"><span> id </span><span style="font-family:宋体;">被点击到的项目的行</span>id</p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若有定义好的</span>OnItemClickListener<span style="font-family:宋体;">被成功调用,则返回</span>true<span style="font-family:宋体;">;否则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean removeFooterView (View v)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">删除之前加入的某个页脚视图。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> v </span><span style="font-family:宋体;">要删除的视图</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若视图被成功删除,则返回</span>true<span style="font-family:宋体;">;若此视图不是页脚视图,则返回</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean removeHeaderView (View v)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">删除之前加入的某个页眉视图。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> v </span><span style="font-family:宋体;">要删除的视图</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">若视图被成功删除,则返回</span>true<span style="font-family:宋体;">;若此视图不是页眉视图,则返回</span>false<span> </span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public boolean requestChildRectangleOnScreen (View child, Rect rect, boolean immediate)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">当组里的某个子项需要被定位在屏幕的某个矩形范围时,调用此方法。</span><span> </span></p> <p align="left"><span> </span><span style="font-family:宋体;">重载此方法的</span>ViewGroup<span style="font-family:宋体;">可确认以下几点:</span></p> <p align="left"><span> </span><span style="font-family:宋体;">·</span><span style="font-family:宋体;">子项目将是组里的直系子项</span></p> <p align="left"><span> </span><span style="font-family:宋体;">·</span><span style="font-family:宋体;">矩形将在子项目的坐标体系中</span></p> <p align="left"><span> </span><span style="font-family:宋体;">重载此方法的</span>ViewGroup<span style="font-family:宋体;">必须保证以下几点:</span></p> <p align="left"><span> </span><span style="font-family:宋体;">·</span><span style="font-family:宋体;">若矩形已经是可见的,则没有东西会改变</span></p> <p align="left"><span> </span><span style="font-family:宋体;">·</span><span style="font-family:宋体;">为使矩形区域全部可见,视图将可以被滚动显示</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> child </span><span style="font-family:宋体;">发出请求的子项目</span></p> <p align="left"><span> rect </span><span style="font-family:宋体;">子项目坐标系内的矩形,即此子项目希望在屏幕上的定位</span></p> <p align="left"><span> immediate </span><span style="font-family:宋体;">设为</span>true<span style="font-family:宋体;">,则禁止动画和缓释移动滚动条</span></p> <p align="left"><span> </span><span style="font-family:宋体;">返回</span></p> <p align="left"><span> </span><span style="font-family:宋体;">这个可滚动显示的组,是否接受请求</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setAdapter (ListAdapter adapter) </span></p> <p align="left"><span> </span><span style="font-family:宋体;">设置</span>ListView<span style="font-family:宋体;">背后的数据。根据</span>ListView<span style="font-family:宋体;">目前使用的特性,</span>adapter<span style="font-family:宋体;">可能被</span>WrapperListAdapter<span style="font-family:宋体;">收起。例如:加页眉和</span>/<span style="font-family:宋体;">或页脚会使</span>adapter<span style="font-family:宋体;">被收起。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> adapter </span><span style="font-family:宋体;">负责维护列表背后的数据,以及生成视图来显示数据里的项目</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> getAdapter() </span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setCacheColorHint (int color)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">当</span>color<span style="font-family:宋体;">的值不为</span>0<span style="font-family:宋体;">时,此值表示的颜色将提示使用者,列表正在一片单色不透明的背景上被画出。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> color </span><span style="font-family:宋体;">背景色</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setChoiceMode (int choiceMode)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">设置</span>List<span style="font-family:宋体;">的选择模式。缺省情况下,列表没有选择模式(即值为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">)。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> choiceMode </span><span style="font-family:宋体;">值可为</span>CHOICE_MODE_NONE<span style="font-family:宋体;">,</span>CHOICE_MODE_NONE<span style="font-family:宋体;">和</span>CHOICE_MODE_NONE<span style="font-family:宋体;">中的一种</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setDivider (Drawable divider) </span></p> <p align="left"><span> </span><span style="font-family:宋体;">设置将画在列表中每个项目之间的图形。如果图形没有已设定好的高度,则必须同时调用</span>setDividerHeight(int)<span style="font-family:宋体;">。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> divider </span><span style="font-family:宋体;">将用作分隔符的图形</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setDividerHeight (int height)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">设置分隔符(画在列表中每个项目之间)的高度。调用此方法将覆盖由</span>setDivider(Drawable)<span style="font-family:宋体;">设置的高度。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> height </span><span style="font-family:宋体;">分隔符的新高度,单位为像素</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setFooterDividersEnabled (boolean footerDividersEnabled)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">设置可以或者不可以为页脚视图画上分隔符。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> headerDividersEnabled </span><span style="font-family:宋体;">设为</span>true<span style="font-family:宋体;">,表明可以画;设为</span>false<span style="font-family:宋体;">则不可以</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> setHeaderDividerEnabled(boolean)</span></p> <p align="left"><span> addFooterView(android.view.View)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setHeaderDividersEnabled (boolean headerDividersEnabled)</span></p> <p align="left"><span> </span><span style="font-family:宋体;">设置可以或者不可以为页眉视图画上分隔符。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> headerDividersEnabled </span><span style="font-family:宋体;">设为</span>true<span style="font-family:宋体;">,表明可以画;设为</span>false<span style="font-family:宋体;">则不可以</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参见</span></p> <p align="left"><span> setFooterDividerEnabled(boolean)</span></p> <p align="left"><span> addHeaderView(android.view.View)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setItemChecked (int position, boolean value)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">设置</span>position<span style="font-family:宋体;">所指定项目的选择状态。只有选择模式为</span>CHOICE_MODE_SINGLE<span style="font-family:宋体;">或者</span>CHOICE_MODE_MULTIPLE<span style="font-family:宋体;">时,此设置才有效。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> position </span><span style="font-family:宋体;">需要改变选择状态的项目的索引</span></p> <p align="left"><span> value </span><span style="font-family:宋体;">新的选择状态</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setItemsCanFocus (boolean itemsCanFocus)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">表明在由</span>ListAdapter<span style="font-family:宋体;">创建的视图中,可包含能获得焦点的项目。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> itemsCanFocus </span><span style="font-family:宋体;">若项目能获得焦点,则设为</span>true<span style="font-family:宋体;">;否则为</span>false</p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setSelection (int position)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">选中</span>position<span style="font-family:宋体;">指定的项目。若为触摸模式,则指定项目不会被选中,但位置变化一样。若</span>position<span style="font-family:宋体;">的值小于</span>0<span style="font-family:宋体;">,则</span>position<span style="font-family:宋体;">为</span>0<span style="font-family:宋体;">的项目将被选中。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> position </span><span style="font-family:宋体;">需要选中的项目的索引(从</span>0<span style="font-family:宋体;">开始)</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setSelectionAfterHeaderView ()</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">选中页眉视图下的第一个列表项目。</span></p> <p style="text-align:left;text-indent:21pt;" align="left"> <span style="background:#d9d9d9;">public void setSelectionFromTop (int position, int y)</span> </p> <p align="left"><span> </span><span style="font-family:宋体;">选中</span>position<span style="font-family:宋体;">指定的项目,并将所选项置于距离</span>ListView<span style="font-family:宋体;">顶端</span>y<span style="font-family:宋体;">像素的位置(若为触摸模式,则指定项目不会被选中,但位置变化一样)。</span></p> <p align="left"><span> </span><span style="font-family:宋体;">参数</span></p> <p align="left"><span> position </span><span style="font-family:宋体;">需要选中的项目的索引(从</span>0<span style="font-family:宋体;">开始)</span></p> <p><span style="font-family:'Calibri','sans-serif';font-size:10.5pt;"> y </span><span style="font-family:宋体;font-size:10.5pt;">距离</span><span style="font-family:'Calibri','sans-serif';font-size:10.5pt;">ListView</span><span style="font-family:宋体;font-size:10.5pt;">(包括间隙)顶端的位置</span></p> <p> </p> <p><span style="font-family:宋体;"><5>参考链接</span></p> <p><span style="font-family:宋体;"> <a href="/misc/goto?guid=4959517929747302906" target="_blank">Android API 中文 (42) —— ListView</a>(大部分引用来自此文)</span></p> <p><span style="font-family:宋体;"> <a href="/misc/goto?guid=4959517929833674421" target="_blank">Android开发之ListView页眉页脚效果VS android背景渐变</a></span></p> <p><span style="font-family:宋体;"> <a href="/misc/goto?guid=4959517929914558637" target="_blank">QQ 多级列表的实现</a></span></p> <p><span style="font-family:宋体;"> <a href="/misc/goto?guid=4959517929988467725" target="_blank">Android 中 ListView 分页加载数据</a></span></p> <p><span style="font-family:宋体;"> <a href="/misc/goto?guid=4959517929988467725" target="_blank">Android ExpandableListView 展开列表控件(手机QQ好友列表)</a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930078366528">Android<span style="font-family:宋体;">入门第六篇之</span>ListView (<span style="font-family:宋体;">一</span>)</a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930155700593">android ListView<span style="font-family:宋体;">详解</span></a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930231220920">android<span style="font-family:宋体;">异步加载</span>ListView<span style="font-family:宋体;">中的图片</span></a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930319491735">Google I/O 2010 - The world of ListView</a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930400029558">Android: <span style="font-family:宋体;">显示</span>SD<span style="font-family:宋体;">卡文件列表</span></a></span></p> <p align="left"><span> <a href="/misc/goto?guid=4959517930487602649">Android: <span style="font-family:宋体;">带图标的</span>ListView<span style="font-family:宋体;">实现</span></a></span></p> <div> <span style="font-family:'Calibri','sans-serif';font-size:10.5pt;"><a href="/misc/goto?guid=4959517930564395423">ListView<span style="font-family:宋体;">和</span>getView<span style="font-family:宋体;">的原理+如何在</span>ListView<span style="font-family:宋体;">中放置多个</span>item</a></span> </div> <p><6>范例<img title="0_1328246868m82t.gif.jpg" border="0" alt="0_1328246868m82t.gif.jpg" src="https://simg.open-open.com/show/fe9ae66b763631dc7c075e1deb489921.jpg" width="414" height="641" /></p> <pre class="brush:java; toolbar: true; auto-links: false;">package xiaosi.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.SimpleAdapter; public class ListView extends ListActivity { /** Called when the activity is first created. */ private ArrayList<Map<String,Object>> list = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] Headline = new String[]{"logo","ID","user"}; int[] ViewID = new int[]{R.id.logo,R.id.ID,R.id.user}; list = new ArrayList<Map<String,Object>>(); Map<String,Object> hashmap1 = new HashMap<String,Object>(); Map<String,Object> hashmap2 = new HashMap<String,Object>(); Map<String,Object> hashmap3 = new HashMap<String,Object>(); Map<String,Object> hashmap4 = new HashMap<String,Object>(); Map<String,Object> hashmap5 = new HashMap<String,Object>(); hashmap1.put("logo",R.drawable.a); hashmap1.put("ID","X000001"); hashmap1.put("user","响铃"); hashmap2.put("logo",R.drawable.b); hashmap2.put("ID","X000002"); hashmap2.put("user","记事本"); hashmap3.put("logo",R.drawable.c); hashmap3.put("ID","X000003"); hashmap3.put("user","音乐"); hashmap4.put("logo",R.drawable.d); hashmap4.put("ID","X000004"); hashmap4.put("user","系统"); hashmap5.put("logo",R.drawable.e); hashmap5.put("ID","X000005"); hashmap5.put("user","警告"); list.add(hashmap1); list.add(hashmap2); list.add(hashmap3); list.add(hashmap4); list.add(hashmap5); SimpleAdapter simpleadapter = new SimpleAdapter(this, list, R.layout.user,Headline, ViewID); setListAdapter(simpleadapter); } @Override protected void onListItemClick(android.widget.ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); System.out.println("position:" + position); System.out.println("id:" + id); } }</pre>main.xml <pre class="brush:xml; toolbar: true; auto-links: false;"><?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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:choiceMode="multipleChoice"/> </LinearLayout></pre>user.xml <pre class="brush:xml; toolbar: true; auto-links: false;"><?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:orientation="horizontal" > <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/ID" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/user" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout></pre> <img title="rar.png" src="https://simg.open-open.com/show/42a8c3e06b0df6f814686984be88c6b4.png" /> <a title="listview范例.rar" href="/misc/goto?guid=4959517930644110606">listview范例.rar</a> <p></p>