ListView
官方:http://developer.android.com/reference/android/widget/ListView.html
相关博客:
类信息:public class ListView extends AbsListView
类继承结构:java.lang.Object—>android.view.View—>android.view.ViewGroup—>android.widget.AdapterView<Textends android.widget.Adapter>—>android.widget.AbsListView—>android.widget.ListView
直接子类:ExpandableListView
简介:显示一个竖直滑动的列表,子项视图来自于ListAdapter。
翻译:
嵌套类:ListView.FixedViewInfo 这个类用来生成一个ListView的“头视图headerat the top”或者“尾视图footer at the bottom”。
xml属性:
属性名 | 描述 |
listview子项间的分割线 | |
分割线高度 | |
array数组资源的引用用来填充ListView | |
在尾视图footer view之前是否填充分割线 | |
在头视图header view之前是否填充分割线 |
继承自AbsListView的xml属性
属性名 | 描述 |
说明这个ListView视图将会永远最优先被绘制,使用一个纯色背景。 | |
定义View的选择行为 | |
当设置为true时, selector将在选择的子项之上绘制。 可以通过setDrawSelectorOnTop(boolean)方法设置 | |
允许快速滑动 | |
标识选择项的图示 可以通过setSelector(int)设置 | |
设置为true时,滑动中将使用绘制的缓存 | |
设置为true时,列表将基于可见子项的高度使用更加智能的计算方法以看起来更加平滑 可以通过setSmoothScrollbarEnabled(boolean)设置 | |
Used by ListView and GridView to stack their content from the bottom. | |
When set to true, the list will filter results as the user types. | |
设置副本 |
继承制ViewGroup的属性
属性名 | 描述 |
设置这个ViewGroup的状态是否包含它子项的绘制状态 Sets whether this ViewGroup's drawable states also include its children's drawable states. | |
定义ViewGroup是否使用当前绘制缓存来绘制它的子项 Defines whether the ViewGroup should always draw its children using their drawing cache or not. | |
定义改变ViewGroup布局时是否需要动画 setLayoutTransition(LayoutTransition)Defines whether changes in layout (caused by adding and removing items) should cause a LayoutTransition to run. | |
Defines whether layout animations should create a drawing cache for their children. | |
setClipChildren(boolean)Defines whether a child is limited to draw inside of its bounds or not. | |
setClipToPadding(boolean)Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. | |
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. | |
Defines the layout animation to use the first time the ViewGroup is laid out. | |
setLayoutMode(int)Defines the layout mode of this ViewGroup. | |
Defines the persistence of the drawing cache. | |
公共方法:
1、 public void addFooterView (View view, Object data, booleanisSelectable)
public void addFooterView (View v)类似
在ListView尾部添加一个View。àandroid4.4之前,只能在setAdapter方法之前调用这个方法;android4.4以后可以在任何时候调用这个方法。
参数:view:需要添加的view
data:与view关联的数据
isSelected:如果这个footer view是可选的,则设置为true
2、 public void addHeaderView (View v, Object data, booleanisSelectable)
public void addHeaderView (View v)类似
设置Header view头视图,用法与addFooterView类似。
3、public booleanareFooterDividersEnabled () add inandroid4.4
返回footer views是否需要绘制。可以通过setFooterDividersEnabled(boolean)设置。
public booleanareHeaderDividersEnabled ()类似。
3、 public boolean dispatchKeyEvent (KeyEvent event)
keyevent是从viewtree的根节点一级一级向下传递的,这个方法用来对keyevent进行分发。如果这个view获取了焦点,它将会把这个keyevent分发给自己,否则向下一级传递。
返回值:如果已经处理这个事件,则返回true;否则返回false。
4、 public ListAdapter getAdapter ()
返回ListView当前的adapter。他可能与setadapter中传入的不同。
5、 public Drawable getDivider ()
6、 public int getDividerHeight ()
7、 public int getFooterViewsCount ()
返回列表中footer view的数量。 footer view是底部的特殊视图,布局时它不应该被循环使用。
public int getHeaderViewsCount ()类似。
8、 public int getMaxScrollAmount ()
返回一次向下滑动事件滑动的view最大数量
9、 public Drawable getOverscrollFooter ()
在所有列表内容后面的drawable。
10、public DrawablegetOverscrollHeader ()
在所有列表内容前显示的drawable。
11、public booleanisOpaque ()
返回值说明试图是否是不透明的。 子类在需要说明对象是否为透明的时候应该覆盖这个方法
返回值:如果视图是完全不透明的则返回true。
12、public voidonInitializeAccessibilityEvent (AccessibilityEvent event) add in android 4.0
初始化view的AccessibilityEvent对象。
覆盖时应该始终调用super.onInitializeAccessibilityEvent (AccessibilityEvent event),然后在此方法中为event设置其他属性。
如果已经通过setAccessibilityDelegate(AccessibilityDelegate)设置View.AccessibilityDelegate,那么onInitializeAccessibilityEvent(View,AccessibilityEvent)有责任来处理这个事件。
13、public voidonInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
初始化view的AccessibilityNodeInfo对象。默认的实现设置以下属性:setParent(View),setBoundsInParent(Rect),setBoundsInScreen(Rect),setPackageName(CharSequence),setClassName(CharSequence),setContentDescription(CharSequence),setEnabled(boolean),setClickable(boolean),setFocusable(boolean),setFocused(boolean),setLongClickable(boolean),setSelected(boolean)
子类覆盖此方法时应该调用默认的实现,设置其余的属性。
如果已经通过setAccessibilityDelegate(AccessibilityDelegate)设置View.AccessibilityDelegate,那么onInitializeAccessibilityNodeInfo(View,AccessibilityNodeInfo)有责任来处理这个事件。
14、public booleanonKeyDown (int keyCode, KeyEvent event)
当这个view是enabled和clickable时,默认的实现是KeyEvent.Callback.onKeyDown()。
软键盘的选择事件不会触发这个监听器。
15、public booleanonKeyMultiple (int keyCode, int repeatCount, KeyEvent event)
默认的实现是KeyEvent.Callback.onKeyMultiple(),总是返回false。
软键盘的选择事件不会触发这个监听器。
16、public booleanonKeyUp (int keyCode, KeyEvent event)
默认的实现是KeyEvent.Callback.onKeyUp()。
软键盘的选择事件不会触发这个监听器。
17、public booleanremoveFooterView (View v)
public booleanremoveHeaderView (View v)
18、public booleanrequestChildRectangleOnScreen (View child, Rect rect, boolean immediate)
Rectangle翻译:矩形
当这个viewgroup的子view需要一个特定的矩形时被调用。
ViewGroup覆盖这个方法需要确保:
1、 子View是这个ViewGroup的直接子视图
2、 举行应该是在子view的坐标zhong
ViewGroup覆盖这个方法需要保持:
1、 这个矩形区域不会改变
2、 子视图可以被滚动直到进这个矩形可见
19、public voidsetAdapter (ListAdapter adapter)
设置ListView关联的数据源。发送给ListView的adapter可能被封装为WrapperListAdapter,这取决于listview当前使用的功能。比如当liatview添加了footerview或Header view是,这个adapter就会被封装为WrapperListAdapter。与之对应的getAdapter方法获得的adapter将是这个WrapperListAdapter对象。
20、public voidsetCacheColorHint (int color)
设置一个纯色(可能是透明的)的默认背景。它不会影响这个view中已存在的背景。
21、public voidsetDivider (Drawable divider)
public voidsetDividerHeight (int height)
22、public voidsetFooterDividersEnabled (boolean footerDividersEnabled)
public voidsetHeaderDividersEnabled (boolean headerDividersEnabled)
23、public voidsetItemsCanFocus (boolean itemsCanFocus)
设置Listview子项是否可获得焦点。
24、public voidsetOverscrollFooter (Drawable footer)
设置将在所有子项后面显示的footerview视图。当用户滚动列表时或子项不会填充满屏幕时,它是可见的。
25、public voidsetOverscrollHeader (Drawable header)
设置在所有子项之前现实的headerview视图。当用户滚动列表时可见。
26、public voidsetRemoteViewsAdapter (Intent intent) add in android 3.0
设置该 ListView,使其使用通过指定的意图连接到 RemoteViewsService 的远程视图适配器.
27、public voidsetSelection (int position)
设置选择项
28、public voidsetSelectionAfterHeaderView ()
选择第一个非列表头视图的子项。
29、setSelectionFromTop(intposition, int y)
选中指定的子项,并将所选条目置于距离ListView 顶端 y 像素处.
30、public voidsmoothScrollByOffset (int offset)
平滑滚动到适配器的指定相对位置.视图会滚动,以使指定位置的视图显示出来.
31、public voidsmoothScrollToPosition (int position)
平滑滚动到适配器的指定位置.视图会滚动,以使指定位置的视图显示出来.
受保护的方法
1、 protected boolean canAnimate ()
说明ViewGroup在首次布局后为其子视图提供动画效果的显示。
2、 protected void dispatchDraw (Canvas canvas)
用来控制子视图的绘制。派生类覆盖此方法以在listview自己完成绘制后去控制子项的绘制。
3、 protected boolean drawChild (Canvas canvas, View child, longdrawingTime)
绘制该视图组的一个子视图.该方法负责取得处于正确状态的画布. 它包含剪切、转换子视图的滚动坐标为(0,0),并应用切换动画.
4、 protected View findViewTraversal (int id)
查找子视图中ID 对应的视图,没找到返回空.
protected View findViewWithTagTraversal (Object tag)类似。
5、 protected void layoutChildren ()
子类必须重写这个方法去布局子视图。
6、 protected void onFinishInflate ()
根据 XML 生成视图工作完成.该函数在生成视图的最后调用,在所有子视图添加完之后.
即使子类覆盖了onFinishInflate 方法,也应该调用父类的方法,使该方法得以执行.
7、 protected void onFocusChanged (boolean gainFocus, int direction,Rect previouslyFocusedRect)
当视图的焦点改变时调用.由定向导航导致的焦点变更时, direction 和 previouslyFocusedRect 提供了焦点是从那里来的进一步信息.
参数:gainFocus 如果视图具有焦点,值为真;否则为假.
direction 当调用requestFocus() 为该视图设置焦点时,该值为焦点移动的方向. 其值为 FOCUS_UP、FOCUS_DOWN、 FOCUS_LEFT、FOCUS_RIGHT、 FOCUS_FORWARD 或者 FOCUS_BACKWARD。当使用无参数的 requestFocus() 时,可能无值.
previouslyFocusedRect 失去焦点的视图的矩形坐标,使用该视图的坐标系统.如果指定了,它将传入可以知道焦点来自哪里的详细信息(作为对 direction 的补充). 否则,其值为 null.
8、 protected void onMeasure (int widthMeasureSpec, intheightMeasureSpec)
计算view和它的内容的宽和高。这个方法被measure(int,int)方法调用,子类可以覆盖以更精确、更有效率的衡量其内容尺寸的方法.
约定:子类覆盖这个方法是,必须调用setMeasuredDimension(int, int)来保存计算的宽和高。否则,measure(int,int)将抛出IllegalStateException异常。调用super.onMeasure(int,int)是有效的方法。
默认的实现计算view背景的尺寸,除非 MeasureSpec 允许超过背景.子类应该重写onMeasure(int,int)以对它的内容提供更好的尺寸。
如果重写了该方法,子类要确保其高度和宽度大于等于视图的最小高度和宽度,通过getSuggestedMinimumHeight()与getSuggestedMinimumWidth()获得。
9、 protected void onSizeChanged (int w, int h, int oldw, int oldh)
该方法在布局期间,当视图的大小变更时调用.如果只是将视图加入视图树,变更前的值为 0.