listView属性及方法小析

           关于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

属性值必须设置为truefalse

可以用"@[package:]type:name

"或者"?[package:][type:]name"(主题属性)的格式来指向某个包含此类型值的资源。

这个属性需要配合addFooterView这个方法使用,此属性缺省值为true。这个属性只对页脚起作用,如果listview中没有添加页脚这个属性就不起作用。

跟这个属性相关的方法:setFooterDividersEnabled()对其进行设置。headerDividersEnabled和这个属性用法基本一致,就是footer是页脚,head是listview的头部。同时,也有setHeaderDividersEnabled()方法。

其方法列表如下:

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.
addHeaderView(),addFooterView()添加头部图标和添加底部图标,可以添加多个,因为它提供了getHeadViewCount()方法 大笑

当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)等。

Delphi的ListView是一种常用的窗体控件,可以在窗体上显示列表数据。它有多个属性可以设置,以实现各种不同的需求。 其中一些常见的属性包括: 1. Columns(列):通过Columns属性可以设置ListView的列数、宽度和标题等。可以使用Columns.Add方法添加列,并设置列的各个属性,如宽度、对齐方式等。 2. Items(项):Items属性用于操作ListView中的项。可以使用Items.Add方法添加项,并设置项的各个属性,如图标、文字等。每个项可以通过SubItems属性设置子项的内容。 3. ViewStyle(视图样式):ViewStyle属性决定了ListView的显示样式。可以设置为vsIcon(图标)、vsSmallIcon(小图标)、vsList(列表)、vsReport(报表)等不同的样式。 4. MultiSelect(多选):MultiSelect属性决定了用户是否可以同时选择多个项。设置为True时,可以通过Shift和Ctrl键来实现多选。 5. ReadOnly(只读):ReadOnly属性决定了用户是否可以修改ListView的内容。设置为True时,用户无法编辑和删除项。 6. OnClick(点击事件):OnClick是ListView的点击事件,当用户点击某个项时触发。可以通过编写OnClick事件处理程序来实现自定义的点击逻辑。 7. OnDblClick(双击事件):OnDblClick是ListView的双击事件,当用户双击某个项时触发。可以通过编写OnDblClick事件处理程序来实现双击项的逻辑。 通过设置这些属性,开发者可以灵活地控制ListView的显示和行为,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值