关于listview的小例子,以前也写过一个,http://blog.csdn.net/aomandeshangxiao/article/details/6643831,今天的这篇文章主要是说一下listview里面的方法和属性,内容均来自:http://developer.android.com/reference/android/widget/ListView.html。摘取了一下自己认为重要的方法。
首先,看下xml中的属性,divider:
Drawable or color to draw between list items.
规定List项目之间用某个图形或颜色来分隔。可以用"@[+] [package:]type:name"或者"?[package:][type:]name"(主题属性)的形式来指向某个已有资源;也可以用"#rgb","#argb","#rrggbb"或者"#aarrggbb"的格式来表示某个颜色。
可参看全局属性资源符号divider。
就是listview的item之间的分割线,可以设置为图片或者设置为一种颜色,在xml文件中,可以设置它的颜色或者图片,在代码中只提供了设置图片的方法:
setDivider (Drawable divider)
和设置分割线高度的方法:
setDividerHeight (int height)
第二个属性:entries
Reference to an array resource that will populate the ListView
引用一个将使用在此ListView里的数组。若数组是固定的,使用此属性将比在程序中写入更为简单。
必须以"@[+][package:]type:name"或者 "?[package:][type:]name"的形式来指向某个资源。
跟这个属性相关的方法就是使用setAdapter(),关于这个方法,在这里就不累述了。
第三个属性:footerDividersEnabled
When set to false, the ListView will not draw the divider before each footer view
设成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
android:id="@+id/myListView01"
android:layout_width="fill_parent"
android:layout_height="287dip"
android:fadingEdge="none"
android:divider="@drawable/list_driver"
android:scrollingCache="false"
android:background="@drawable/list">
</ListView>
以上三个问题均来自网络。当然,也可以在java代码中使用方法来解决,上面都有提及,如setDivider()、setCacheColorHint(0)等。