关于listview的小例子,以前也写过一个,http://blog.csdn.net/aomandeshangxiao/article/details/6643831,今天的这篇文章主要是说一下listview里面的方法和属性,内容均来自:http://developer.android.com/reference/android/widget/ListView.html。摘取了一下自己认为重要的方法。
首先,看下xml中的属性,divider:
规定List项目之间用某个图形或颜色来分隔。可以用"@[+] [package:]type:name"或者"?[package:][type:]name"(主题属性)的形式来指向某个已有资源;也可以用"#rgb","#argb","#rrggbb"或者"#aarrggbb"的格式来表示某个颜色。
可参看全局属性资源符号divider。
就是listview的item之间的分割线,可以设置为图片或者设置为一种颜色,在xml文件中,可以设置它的颜色或者图片,在代码中只提供了设置图片的方法:
和设置分割线高度的方法:
第二个属性:entries
引用一个将使用在此ListView里的数组。若数组是固定的,使用此属性将比在程序中写入更为简单。
必须以"@[+][package:]type:name"或者 "?[package:][type:]name"的形式来指向某个资源。
跟这个属性相关的方法就是使用setAdapter(),关于这个方法,在这里就不累述了。
第三个属性:footerDividersEnabled
设成flase时,此ListView将不会在页脚视图前画分隔符。此属性缺省值为true。
属性值必须设置为true或false。
可以用"@[package:]type:name
"或者"?[package:][type:]name"(主题属性)的格式来指向某个包含此类型值的资源。
这个属性需要配合addFooterView这个方法使用,此属性缺省值为true。这个属性只对页脚起作用,如果listview中没有添加页脚这个属性就不起作用。
其方法列表如下:
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void | addFooterView(View v)
Add a fixed view to appear at the bottom of the list.
| ||||||||||
void | addFooterView(View v, Object data, boolean isSelectable)
Add a fixed view to appear at the bottom of the list.
| ||||||||||
void | addHeaderView(View v, Object data, boolean isSelectable)
Add a fixed view to appear at the top of the list.
| ||||||||||
void | addHeaderView(View v)
Add a fixed view to appear at the top of the list.
| ||||||||||
boolean | dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
| ||||||||||
ListAdapter | getAdapter()
Returns the adapter currently in use in this ListView.
| ||||||||||
long[] | getCheckItemIds()
This method is deprecated. Use getCheckedItemIds() instead.
| ||||||||||
Drawable | getDivider()
Returns the drawable that will be drawn between each item in the list.
| ||||||||||
int | getDividerHeight() | ||||||||||
int | getFooterViewsCount()
Returns the number of footer views in the list.
| ||||||||||
int | getHeaderViewsCount()
Returns the number of header views in the list.
| ||||||||||
boolean | getItemsCanFocus() | ||||||||||
int | getMaxScrollAmount() | ||||||||||
Drawable | getOverscrollFooter() | ||||||||||
Drawable | getOverscrollHeader() | ||||||||||
boolean | isOpaque()
Indicates whether this View is opaque.
| ||||||||||
boolean | onKeyDown(int keyCode, KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyDown() : perform press of the view when
KEYCODE_DPAD_CENTER or
KEYCODE_ENTER is released, if the view is enabled and clickable.
| ||||||||||
boolean | onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyMultiple() : always returns false (doesn't handle the event).
| ||||||||||
boolean | onKeyUp(int keyCode, KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyUp() : perform clicking of the view when
KEYCODE_DPAD_CENTER or
KEYCODE_ENTER is released.
| ||||||||||
boolean | removeFooterView(View v)
Removes a previously-added footer view.
| ||||||||||
boolean | removeHeaderView(View v)
Removes a previously-added header view.
| ||||||||||
boolean | requestChildRectangleOnScreen(View child, Rect rect, boolean immediate)
Called when a child of this group wants a particular rectangle to be positioned onto the screen.
| ||||||||||
void | setAdapter(ListAdapter adapter)
Sets the data behind this ListView.
| ||||||||||
void | setCacheColorHint(int color)
When set to a non-zero value, the cache color hint indicates that this list is always drawn on top of a solid, single-color, opaque background.
| ||||||||||
void | setDivider(Drawable divider)
Sets the drawable that will be drawn between each item in the list.
| ||||||||||
void | setDividerHeight(int height)
Sets the height of the divider that will be drawn between each item in the list.
| ||||||||||
void | setFooterDividersEnabled(boolean footerDividersEnabled)
Enables or disables the drawing of the divider for footer views.
| ||||||||||
void | setHeaderDividersEnabled(boolean headerDividersEnabled)
Enables or disables the drawing of the divider for header views.
| ||||||||||
void | setItemsCanFocus(boolean itemsCanFocus)
Indicates that the views created by the ListAdapter can contain focusable items.
| ||||||||||
void | setOverscrollFooter(Drawable footer)
Sets the drawable that will be drawn below all other list content.
| ||||||||||
void | setOverscrollHeader(Drawable header)
Sets the drawable that will be drawn above all other list content.
| ||||||||||
void | setRemoteViewsAdapter(Intent intent)
Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService through the specified intent.
| ||||||||||
void | setSelection(int position)
Sets the currently selected item.
| ||||||||||
void | setSelectionAfterHeaderView()
setSelectionAfterHeaderView set the selection to be the first list item after the header views.
| ||||||||||
void | setSelectionFromTop(int position, int y)
Sets the selected item and positions the selection y pixels from the top edge of the ListView.
| ||||||||||
void | smoothScrollByOffset(int offset)
Smoothly scroll to the specified adapter position offset.
| ||||||||||
void | smoothScrollToPosition(int position)
Smoothly scroll to the specified adapter position.
|
当listView定长滑动时,顶端会出现黑边,影响效果,可以使用setCacheColorHint(0)这个方法。其他的方法,直接从字面意思上大体就可以明白的差不多,不多说了。
常遇到的问题及方法:
1.、listview在拖动的时候背景图片消失变成黑色背景。等到拖动完毕我们自己的背景图片才显示出来。
2 、listview的上边和下边有黑色的阴影。
3、lsitview的每一项之间需要设置一个图片做为间隔。
针对以上问题 在listview的xml文件中设置一下语句。
问题1 有如下代码结解决 Android:scrollingCache="false"
问题2 用如下代码解决:android:fadingEdge="none"
问题3 用如下代码解决: android:divider="@drawable/list_driver" 其中 @drawable/list_driver 是一个图片资源
总体如下
listview 之XML属性
<ListView android:id="@id/list_temple_container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" android:cacheColorHint="#00000000" android:divider="@drawable/list_divider" android:dividerHeight="1dip" android:fastScrollEnabled="false" android:headerDividersEnabled="true" android:listSelector="#00000000" android:scrollingCache="true" />
list_divider
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="0" android:centerColor="#ffe6e6e6" android:endColor="#ffe6e6e6" android:height="1px" android:startColor="#ffe6e6e6" /> </shape>
android:divider="@drawable/list_divider" item之间的分割线
android:dividerHeight="1dip" item之间的分割线高度
android:fastScrollEnabled="false"
android:headerDividersEnabled="true"
android:cacheColorHint="#00000000" 系统切换item时的缓存色
android:scrollbars="none" 不显示滚动条
android:listSelector="#00000000"选中时背景色
通过设置这样的属性可以做出更加美观的列表
首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false
android:stackFromBottom="true"
第二是transciptMode属性,需要用ListView或者其它显示大量Items的控件实时跟踪或者查看信息,并且希望最新的条目可以自动滚动到可视范围内。通过设置的控件transcriptMode属性可以将Android平台的控件(支持ScrollBar)自动滑动到最底部。
android:transcriptMode="alwaysScroll"
第三cacheColorHint属性,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android:background="@drawable/bg",不过不要高兴地太早,当你这么做以后,发现背景是变了,但是当你拖动,或者点击list空白位置的时候发现ListItem都变成黑色的了,破坏了整体效果。
如果你只是换背景的颜色的话,可以直接指定android:cacheColorHint为你所要的颜色,如果你是用图片做背景的话,那也只要将android:cacheColorHint指定为透明(#00000000)就可以了
第四divider属性,该属性作用是每一项之间需要设置一个图片做为间隔,或是去掉item之间的分割线
android:divider="@drawable/list_driver" 其中 @drawable/list_driver 是一个图片资源,如果不想显示分割线则只要设置为android:divider="@drawable/@null" 就可以了
第五fadingEdge属性,上边和下边有黑色的阴影
android:fadingEdge="none" 设置后没有阴影了~
第六scrollbars属性,作用是隐藏listView的滚动条,
android:scrollbars="none"与setVerticalScrollBarEnabled(true);的效果是一样的,不活动的时候隐藏,活动的时候也隐藏
第七fadeScrollbars属性,android:fadeScrollbars="true" 配置ListView布局的时候,设置这个属性为true就可以实现滚动条的自动隐藏和显示。
android:scrollbars="vertical"
android:background="@null"
android:headerDividersEnabled="true"
android:listSelector="@drawable/x_room_listitem"
这些个呢???
android:listSelector="@drawable/x_room_listitem" 设置选中颜色
android:footerDividersEnabled 设成flase时,此ListView将不会在页脚视图前画分隔符。此属性缺省值为true
android:headerDividersEnabled设成flase时,此ListView将不会在页眉视图前画分隔符。此属性缺省值为true
若 有别的会把焦点弄走就 点不到了 那要先把那个的焦点给false点 或者
如果是通过ListAdapter返回的view,可以给该view添加属性
android:descendantFocusability="blocksDescendants"
也可在代码中设置此属性,这样就可以将view中子控件的事件阻止,然后ListView就会响应了。
将ListView的背景色设置为白色,ListIView控件上下滑动时,背景就会变为黑色,但是图片会正常显示,这怎么解决呢?网上搜了一下,
如果大家在非黑色背景下使用ListView控件时,Android默认可能在滚动ListView时这个列表控件的背景突然变成黑色。这样可能导致程序的黑色的背景和主程序的主题既不协调。解决的方法Google在设计Android时也考虑了,在Layout的ListView中加入 android:cacheColorHint="#00000000" 的属性即可,或者是调用ListView的setCacheColorHint(0);方法。
在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
在Android软件设计与实现中我们通常都会使用到ListView这个控件,系统有一些预置的Adapter可以使用,例如SimpleAdapter和ArrayAdapter,但是总是会有一些情况我们需要通过自定义ListView来实现一些效果,那么在这个时候,我们通常会碰到自定义ListView无法选中整个ListViewItem的情况,也就是无法响应ListView的onItemClickListener中的onItemClick()方法,
我们看到了一行代码定义的变量的意思是“当前View将屏蔽他所有子控件的Focus状态,即便这些子控件是可以Focus的”,其实这段话的意思就是这个变量代表着当前的View将不顾其子控件是否可以Focus自身接管了所有的Focus,通常默认能获得focus的控件有Button,Checkable继承来的所有控件,这就意味着如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了,也就会响应onItemClickListener中的onItemClick()方法,然而将ListView的Item Layout的子控件focusable属性设置为false有点繁琐,我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendants”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItenClick()方法了。例如我的ListViw的每个item项是RelativeLayout,那么我就可以设置RelativeLayout的android:descendantFocusability=”blocksDescendants”即可。注意:这个属性不能设置给ListView,设置了也不起作用。
第二种方法是将ListView子控件中的CheckBox或者ImageButton,Button的android:focusable="false"属性即可。
开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点。原因多半是由于在你自己定义的Item中存在诸如ImageButton,Button,CheckBox等子控件(也可以说是Button或者Checkable的子类控件),此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。
这时候就可以使用descendantFocusability来解决啦,API描述如下:
android:descendantFocusability
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values.
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点