ListView
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.view.ViewGroup | ||||
↳ | android.widget.AdapterView<T extends android.widget.Adapter> | ||||
↳ | android.widget.AbsListView | ||||
↳ | android.widget.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 | areFooterDividersEnabled() | ||||||||||
boolean | areHeaderDividersEnabled() | ||||||||||
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 was deprecated in API level 8. 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.
| ||||||||||
void |
onInitializeAccessibilityEvent(
AccessibilityEvent event)
Initializes an
AccessibilityEvent with information about this View which is the event source.
| ||||||||||
void |
onInitializeAccessibilityNodeInfo(
AccessibilityNodeInfo info)
Initializes an
AccessibilityNodeInfo with information about this view.
| ||||||||||
void |
onInitializeAccessibilityNodeInfoForItem(
View view, int position,
AccessibilityNodeInfo info)
Initializes an
AccessibilityNodeInfo with information about a particular item in the list.
| ||||||||||
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.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
boolean |
canAnimate()
Indicates whether the view group has the ability to animate its children after the first layout.
| ||||||||||
void |
dispatchDraw(
Canvas canvas)
Called by draw to draw the child views.
| ||||||||||
boolean |
drawChild(
Canvas canvas,
View child, long drawingTime)
Draw one child of this View Group.
| ||||||||||
View | findViewTraversal(int id) | ||||||||||
View | findViewWithTagTraversal( Object tag) | ||||||||||
void |
layoutChildren()
Subclasses must override this method to layout their children.
| ||||||||||
void |
onFinishInflate()
Finalize inflating a view from XML.
| ||||||||||
void |
onFocusChanged(boolean gainFocus, int direction,
Rect previouslyFocusedRect)
Called by the view system when the focus state of this view changes.
| ||||||||||
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
Measure the view and its content to determine the measured width and the measured height. | ||||||||||
void |
onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
|
ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:
1.准备ListView要显示的数据 ;
2.使用 一维或多维 动态数组 保存数据;
2.构建适配器 , 简单地来说, 适配器就是 Item数组 , 动态数组 有多少元素就生成多少个Item;
3.把 适配器 添加到ListView,并显示出来。
UI的XML代码:
main.xml代码如下
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <ListView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/MyListView">
- </ListView>
- </LinearLayout>
my_listitem.xml的代码如下,my_listitem.xml用于设计ListView的Item:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_height="wrap_content"
- android:id="@+id/MyListItem"
- android:paddingBottom="3dip"
- android:paddingLeft="10dip">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/ItemTitle"
- android:textSize="30dip">
- </TextView>
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/ItemText">
- </TextView>
- </LinearLayout>
JAVA的源代码:
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //绑定XML中的ListView,作为Item的容器
- ListView list = (ListView) findViewById(R.id.MyListView);
- //生成动态数组,并且转载数据
- ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
- for(int i=0;i<30;i++)
- {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("ItemTitle", "This is Title.....");
- map.put("ItemText", "This is text.....");
- mylist.add(map);
- }
- //生成适配器,数组===》ListItem
- SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
- mylist,//数据来源
- R.layout.my_listitem,//ListItem的XML实现
- //动态数组与ListItem对应的子项
- new String[] {"ItemTitle", "ItemText"},
- //ListItem的XML文件里面的两个TextView ID
- new int[] {R.id.ItemTitle,R.id.ItemText});
- //添加并且显示
- list.setAdapter(mSchedule);
- }
关于ListView相关的使用,实际使用少不了适配器的操作,以及相关的布局效果实现,参见适配器章节....
android:layout_height
| 必须将ListView的布局高度属性设置“match_parent 或 400dp等绝对数值”, 如果ListView的布局高度为“wrap_content”,那么getView()就会重复调用。一般来说,一个item会被调用三遍左右。 |
android:cacheColorHint="#00000000" |
ListView设置背景,拖动背景色为透明
|
android:dividerHeight="1dp" | item之间的高度 |
android:divider="@drawable/div" android:divider="#FF0" | item之间的背景或者说是颜色 |
android:listSelector="@drawable/list_item_bg"
|
item按下的样式
|
android:scrollbars="none"
|
隐藏滚动条。
只有值为vertical的时候,才会显示滚动条,
并且会自动影藏和显示
|
android:fastScrollEnabled="true" | 拖动滑块,快速滚动 |
android:scrollbarStyle="outsideInset"
| insideOverlay:默认值,表示在padding区域内并且覆盖在view上 insideInset:表示在padding区域内并且插入在view后面 outsideOverlay:表示在padding区域外并且覆盖在view上,推荐这个 outsideInset:表示在padding区域外并且插入在view后面 |