关于listview的小例子,以前也写过一个,http://blog.youkuaiyun.com/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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)" style="color:rgb(0,102,153); text-decoration:none">addFooterView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v)</nobr>
Add a fixed view to appear at the bottom of the list.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View,%20java.lang.Object,%20boolean)" style="color:rgb(0,102,153); text-decoration:none">addFooterView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v,<a href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>data, boolean isSelectable)</nobr>
Add a fixed view to appear at the bottom of the list.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View,%20java.lang.Object,%20boolean)" style="color:rgb(0,102,153); text-decoration:none">addHeaderView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v,<a href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>data, boolean isSelectable)</nobr>
Add a fixed view to appear at the top of the list.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)" style="color:rgb(0,102,153); text-decoration:none">addHeaderView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v)</nobr>
Add a fixed view to appear at the top of the list.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#dispatchKeyEvent(android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">dispatchKeyEvent</a></span>(<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
Dispatch a key event to the next view on the focus path.
| ||||||||||
<nobr><a href="http://developer.android.com/reference/android/widget/ListAdapter.html" style="color:rgb(0,102,153); text-decoration:none">ListAdapter</a></nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getAdapter()" style="color:rgb(0,102,153); text-decoration:none">getAdapter</a></span>()</nobr>
Returns the adapter currently in use in this ListView.
| ||||||||||
<nobr>long[]</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getCheckItemIds()" style="color:rgb(0,102,153); text-decoration:none">getCheckItemIds</a></span>()</nobr>
This method is deprecated. Use getCheckedItemIds() instead.
| ||||||||||
<nobr><a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a></nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getDivider()" style="color:rgb(0,102,153); text-decoration:none">getDivider</a></span>()</nobr>
Returns the drawable that will be drawn between each item in the list.
| ||||||||||
<nobr>int</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getDividerHeight()" style="color:rgb(0,102,153); text-decoration:none">getDividerHeight</a></span>()</nobr> | ||||||||||
<nobr>int</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getFooterViewsCount()" style="color:rgb(0,102,153); text-decoration:none">getFooterViewsCount</a></span>()</nobr>
Returns the number of footer views in the list.
| ||||||||||
<nobr>int</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getHeaderViewsCount()" style="color:rgb(0,102,153); text-decoration:none">getHeaderViewsCount</a></span>()</nobr>
Returns the number of header views in the list.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getItemsCanFocus()" style="color:rgb(0,102,153); text-decoration:none">getItemsCanFocus</a></span>()</nobr> | ||||||||||
<nobr>int</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getMaxScrollAmount()" style="color:rgb(0,102,153); text-decoration:none">getMaxScrollAmount</a></span>()</nobr> | ||||||||||
<nobr><a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a></nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getOverscrollFooter()" style="color:rgb(0,102,153); text-decoration:none">getOverscrollFooter</a></span>()</nobr> | ||||||||||
<nobr><a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a></nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#getOverscrollHeader()" style="color:rgb(0,102,153); text-decoration:none">getOverscrollHeader</a></span>()</nobr> | ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#isOpaque()" style="color:rgb(0,102,153); text-decoration:none">isOpaque</a></span>()</nobr>
Indicates whether this View is opaque.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#onKeyDown(int,%20android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">onKeyDown</a></span>(int keyCode,<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
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.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#onKeyMultiple(int,%20int,%20android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">onKeyMultiple</a></span>(int keyCode, int repeatCount,<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
Default implementation of
KeyEvent.Callback.onKeyMultiple() : always returns false (doesn't handle the event).
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#onKeyUp(int,%20android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">onKeyUp</a></span>(int keyCode,<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
Default implementation of
KeyEvent.Callback.onKeyUp() : perform clicking of the view when
KEYCODE_DPAD_CENTER or
KEYCODE_ENTER is released.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#removeFooterView(android.view.View)" style="color:rgb(0,102,153); text-decoration:none">removeFooterView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v)</nobr>
Removes a previously-added footer view.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#removeHeaderView(android.view.View)" style="color:rgb(0,102,153); text-decoration:none">removeHeaderView</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>v)</nobr>
Removes a previously-added header view.
| ||||||||||
<nobr>boolean</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#requestChildRectangleOnScreen(android.view.View,%20android.graphics.Rect,%20boolean)" style="color:rgb(0,102,153); text-decoration:none">requestChildRectangleOnScreen</a></span>(<a href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); text-decoration:none">View</a>child,<a href="http://developer.android.com/reference/android/graphics/Rect.html" style="color:rgb(0,102,153); text-decoration:none">Rect</a>rect, boolean immediate)</nobr>
Called when a child of this group wants a particular rectangle to be positioned onto the screen.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setAdapter(android.widget.ListAdapter)" style="color:rgb(0,102,153); text-decoration:none">setAdapter</a></span>(<a href="http://developer.android.com/reference/android/widget/ListAdapter.html" style="color:rgb(0,102,153); text-decoration:none">ListAdapter</a>adapter)</nobr>
Sets the data behind this ListView.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setCacheColorHint(int)" style="color:rgb(0,102,153); text-decoration:none">setCacheColorHint</a></span>(int color)</nobr>
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.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setDivider(android.graphics.drawable.Drawable)" style="color:rgb(0,102,153); text-decoration:none">setDivider</a></span>(<a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a>divider)</nobr>
Sets the drawable that will be drawn between each item in the list.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setDividerHeight(int)" style="color:rgb(0,102,153); text-decoration:none">setDividerHeight</a></span>(int height)</nobr>
Sets the height of the divider that will be drawn between each item in the list.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setFooterDividersEnabled(boolean)" style="color:rgb(0,102,153); text-decoration:none">setFooterDividersEnabled</a></span>(boolean footerDividersEnabled)</nobr>
Enables or disables the drawing of the divider for footer views.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setHeaderDividersEnabled(boolean)" style="color:rgb(0,102,153); text-decoration:none">setHeaderDividersEnabled</a></span>(boolean headerDividersEnabled)</nobr>
Enables or disables the drawing of the divider for header views.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setItemsCanFocus(boolean)" style="color:rgb(0,102,153); text-decoration:none">setItemsCanFocus</a></span>(boolean itemsCanFocus)</nobr>
Indicates that the views created by the ListAdapter can contain focusable items.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setOverscrollFooter(android.graphics.drawable.Drawable)" style="color:rgb(0,102,153); text-decoration:none">setOverscrollFooter</a></span>(<a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a>footer)</nobr>
Sets the drawable that will be drawn below all other list content.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setOverscrollHeader(android.graphics.drawable.Drawable)" style="color:rgb(0,102,153); text-decoration:none">setOverscrollHeader</a></span>(<a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); text-decoration:none">Drawable</a>header)</nobr>
Sets the drawable that will be drawn above all other list content.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setRemoteViewsAdapter(android.content.Intent)" style="color:rgb(0,102,153); text-decoration:none">setRemoteViewsAdapter</a></span>(<a href="http://developer.android.com/reference/android/content/Intent.html" style="color:rgb(0,102,153); text-decoration:none">Intent</a>intent)</nobr>
Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService through the specified intent.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)" style="color:rgb(0,102,153); text-decoration:none">setSelection</a></span>(int position)</nobr>
Sets the currently selected item.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setSelectionAfterHeaderView()" style="color:rgb(0,102,153); text-decoration:none">setSelectionAfterHeaderView</a></span>()</nobr>
setSelectionAfterHeaderView set the selection to be the first list item after the header views.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int,%20int)" style="color:rgb(0,102,153); text-decoration:none">setSelectionFromTop</a></span>(int position, int y)</nobr>
Sets the selected item and positions the selection y pixels from the top edge of the ListView.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#smoothScrollByOffset(int)" style="color:rgb(0,102,153); text-decoration:none">smoothScrollByOffset</a></span>(int offset)</nobr>
Smoothly scroll to the specified adapter position offset.
| ||||||||||
<nobr>void</nobr> | <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)" style="color:rgb(0,102,153); text-decoration:none">smoothScrollToPosition</a></span>(int position)</nobr>
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)等。