Most visited

Recently visited

Added in API level 1

AdapterView

public abstract class AdapterView
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AdapterView<T extends android.widget.Adapter>
Known Direct Subclasses
Known Indirect Subclasses


AdapterView是一个视图,子视图由 Adapter决定。

ListViewGridViewSpinnerGallery的适配器视图的常用的子类。

Developer Guides

有关使用AdapterView的更多信息,请阅读 Binding to Data with AdapterView开发人员指南。

Summary

Nested classes

class AdapterView.AdapterContextMenuInfo

当为此AdapterView提供上下文菜单时,提供给onCreateContextMenu(ContextMenu, View, ContextMenuInfo)回调的额外菜单信息。

interface AdapterView.OnItemClickListener

在单击此AdapterView中的项目时调用回调的接口定义。

interface AdapterView.OnItemLongClickListener

在单击并保持此视图中的项目时调用回调的接口定义。

interface AdapterView.OnItemSelectedListener

在此视图中的项目被选中时调用回调的接口定义。

Inherited XML attributes

From class android.view.ViewGroup
From class android.view.View

Constants

int INVALID_POSITION

代表无效的职位。

long INVALID_ROW_ID

表示空的或无效的行ID

int ITEM_VIEW_TYPE_HEADER_OR_FOOTER

项目是页眉或页脚时,由 getItemViewType(int)返回的项目视图类型。

int ITEM_VIEW_TYPE_IGNORE

当适配器不希望回收项目视图时,由 getItemViewType(int)返回的项目视图类型。

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

AdapterView(Context context)
AdapterView(Context context, AttributeSet attrs)
AdapterView(Context context, AttributeSet attrs, int defStyleAttr)
AdapterView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void addView(View child, int index)

此方法不受支持,并在调用时引发UnsupportedOperationException。

void addView(View child)

此方法不受支持,并在调用时引发UnsupportedOperationException。

void addView(View child, ViewGroup.LayoutParams params)

此方法不受支持,并在调用时引发UnsupportedOperationException。

void addView(View child, int index, ViewGroup.LayoutParams params)

此方法不受支持,并在调用时引发UnsupportedOperationException。

CharSequence getAccessibilityClassName()

返回此对象的类名称以用于辅助功能。

abstract T getAdapter()

返回当前与此小部件关联的适配器。

int getCount()
View getEmptyView()

当前适配器为空时,AdapterView可以显示一个称为空视图的特殊视图。

int getFirstVisiblePosition()

返回适配器数据集中屏幕上显示的第一项的位置。

Object getItemAtPosition(int position)

获取与列表中指定位置关联的数据。

long getItemIdAtPosition(int position)
int getLastVisiblePosition()

返回适配器数据集中屏幕上显示的最后一个项目的位置。

final AdapterView.OnItemClickListener getOnItemClickListener()
final AdapterView.OnItemLongClickListener getOnItemLongClickListener()
final AdapterView.OnItemSelectedListener getOnItemSelectedListener()
int getPositionForView(View view)

返回视图的适配器数据集内的位置,其中视图是适配器项目或适配器项目的后代。

Object getSelectedItem()
long getSelectedItemId()
int getSelectedItemPosition()

返回适配器数据集内当前选定项目的位置

abstract View getSelectedView()
boolean performItemClick(View view, int position, long id)

调用OnItemClickListener(如果已定义)。

void removeAllViews()

此方法不受支持,并在调用时引发UnsupportedOperationException。

void removeView(View child)

此方法不受支持,并在调用时引发UnsupportedOperationException。

void removeViewAt(int index)

此方法不受支持,并在调用时引发UnsupportedOperationException。

abstract void setAdapter(T adapter)

设置提供数据的适配器和视图来表示这个小部件中的数据。

void setEmptyView(View emptyView)

设置视图以显示适配器是否为空

void setFocusable(boolean focusable)

设置此视图是否可以获得焦点。

void setFocusableInTouchMode(boolean focusable)

设置此视图是否可以在触摸模式下获得焦点。

void setOnClickListener(View.OnClickListener l)

单击此视图时注册要调用的回调。

void setOnItemClickListener(AdapterView.OnItemClickListener listener)

当单击此AdapterView中的某个项目时,注册要调用的回调。

void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener)

当单击并保持此AdapterView中的某个项目时,注册要调用的回调

void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)

当选择了此AdapterView中的项目时,注册要调用的回调。

abstract void setSelection(int position)

设置当前选择的项目。

Protected methods

boolean canAnimate()

指示视图组是否能够在第一个布局之后为其子项设置动画。

void dispatchRestoreInstanceState(SparseArray<Parcelable> container)

覆盖以防止适配器创建的任何视图解冻。

void dispatchSaveInstanceState(SparseArray<Parcelable> container)

覆盖以防止冻结由适配器创建的任何视图。

void onDetachedFromWindow()

这是在视图从窗口分离时调用的。

void onLayout(boolean changed, int left, int top, int right, int bottom)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。

Inherited methods

From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Constants

INVALID_POSITION

Added in API level 1
int INVALID_POSITION

代表无效的职位。 所有有效位置的范围是0到1,小于当前适配器中的项目数量。

常量值:-1(0xffffffff)

INVALID_ROW_ID

Added in API level 1
long INVALID_ROW_ID

表示空的或无效的行ID

常量值:-9223372036854775808(0x8000000000000000)

ITEM_VIEW_TYPE_HEADER_OR_FOOTER

Added in API level 1
int ITEM_VIEW_TYPE_HEADER_OR_FOOTER

项目为页眉或页脚时,由 getItemViewType(int)返回的项目视图类型。

常量值:-2(0xfffffffe)

ITEM_VIEW_TYPE_IGNORE

Added in API level 1
int ITEM_VIEW_TYPE_IGNORE

当适配器不希望回收项目视图时,由 getItemViewType(int)返回的项目视图类型。

常量值:-1(0xffffffff)

Public constructors

AdapterView

Added in API level 1
AdapterView (Context context)

Parameters
context Context

AdapterView

Added in API level 1
AdapterView (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

AdapterView

Added in API level 1
AdapterView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

AdapterView

Added in API level 21
AdapterView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

Public methods

addView

Added in API level 1
void addView (View child, 
                int index)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
child View: Ignored.
index int: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

addView

Added in API level 1
void addView (View child)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
child View: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

addView

Added in API level 1
void addView (View child, 
                ViewGroup.LayoutParams params)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
child View: Ignored.
params ViewGroup.LayoutParams: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

addView

Added in API level 1
void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
child View: Ignored.
index int: Ignored.
params ViewGroup.LayoutParams: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

getAccessibilityClassName

Added in API level 23
CharSequence getAccessibilityClassName ()

返回此对象的类名称以用于辅助功能。 如果子类正在实现的东西应该被视为一个全新的视图类,当它被可访问性使用时,子类只应该覆盖这个子类,与它所源自的类无关。 这用于填写AccessibilityNodeInfo.setClassName

Returns
CharSequence

getAdapter

Added in API level 1
T getAdapter ()

返回当前与此小部件关联的适配器。

Returns
T The adapter used to provide this view's content.

getCount

Added in API level 1
int getCount ()

Returns
int The number of items owned by the Adapter associated with this AdapterView. (This is the number of data items, which may be larger than the number of visible views.)

getEmptyView

Added in API level 1
View getEmptyView ()

当前适配器为空时,AdapterView可以显示一个称为空视图的特殊视图。 空视图用于向用户提供反馈,表示该AdapterView中没有数据可用。

Returns
View The view to show if the adapter is empty.

getFirstVisiblePosition

Added in API level 1
int getFirstVisiblePosition ()

返回适配器数据集中屏幕上显示的第一项的位置。

Returns
int The position within the adapter's data set

getItemAtPosition

Added in API level 1
Object getItemAtPosition (int position)

获取与列表中指定位置关联的数据。

Parameters
position int: Which data to get
Returns
Object The data associated with the specified position in the list

getItemIdAtPosition

Added in API level 1
long getItemIdAtPosition (int position)

Parameters
position int
Returns
long

getLastVisiblePosition

Added in API level 1
int getLastVisiblePosition ()

返回适配器数据集中屏幕上显示的最后一个项目的位置。

Returns
int The position within the adapter's data set

getOnItemClickListener

Added in API level 1
AdapterView.OnItemClickListener getOnItemClickListener ()

Returns
AdapterView.OnItemClickListener The callback to be invoked with an item in this AdapterView has been clicked, or null id no callback has been set.

getOnItemLongClickListener

Added in API level 1
AdapterView.OnItemLongClickListener getOnItemLongClickListener ()

Returns
AdapterView.OnItemLongClickListener The callback to be invoked with an item in this AdapterView has been clicked and held, or null id no callback as been set.

getOnItemSelectedListener

Added in API level 1
AdapterView.OnItemSelectedListener getOnItemSelectedListener ()

Returns
AdapterView.OnItemSelectedListener

getPositionForView

Added in API level 1
int getPositionForView (View view)

返回视图的适配器数据集内的位置,其中视图是适配器项目或适配器项目的后代。

注意:此方法的结果仅反映在最近的布局过程中绑定以查看的数据的位置。 如果适配器的数据集在没有后续布局传递的情况下发生更改,则此方法返回的位置可能与适配器内数据的当前位置不匹配。

Parameters
view View: an adapter item, or a descendant of an adapter item. This must be visible in this AdapterView at the time of the call.
Returns
int the position within the adapter's data set of the view, or INVALID_POSITION if the view does not correspond to a list item (or it is not currently visible)

getSelectedItem

Added in API level 1
Object getSelectedItem ()

Returns
Object The data corresponding to the currently selected item, or null if there is nothing selected.

getSelectedItemId

Added in API level 1
long getSelectedItemId ()

Returns
long The id corresponding to the currently selected item, or INVALID_ROW_ID if nothing is selected.

getSelectedItemPosition

Added in API level 1
int getSelectedItemPosition ()

返回适配器数据集内当前选定项目的位置

Returns
int int Position (starting at 0), or INVALID_POSITION if there is nothing selected.

getSelectedView

Added in API level 1
View getSelectedView ()

Returns
View The view corresponding to the currently selected item, or null if nothing is selected

performItemClick

Added in API level 1
boolean performItemClick (View view, 
                int position, 
                long id)

调用OnItemClickListener(如果已定义)。 执行与点击相关的所有常规操作:报告辅助功能事件,播放声音等。

Parameters
view View: The view within the AdapterView that was clicked.
position int: The position of the view in the adapter.
id long: The row id of the item that was clicked.
Returns
boolean True if there was an assigned OnItemClickListener that was called, false otherwise is returned.

removeAllViews

Added in API level 1
void removeAllViews ()

此方法不受支持,并在调用时引发UnsupportedOperationException。

Throws
UnsupportedOperationException Every time this method is invoked.

removeView

Added in API level 1
void removeView (View child)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
child View: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

removeViewAt

Added in API level 1
void removeViewAt (int index)

此方法不受支持,并在调用时引发UnsupportedOperationException。

Parameters
index int: Ignored.
Throws
UnsupportedOperationException Every time this method is invoked.

setAdapter

Added in API level 1
void setAdapter (T adapter)

设置提供数据的适配器和视图来表示这个小部件中的数据。

Parameters
adapter T: The adapter to use to create this view's content.

setEmptyView

Added in API level 1
void setEmptyView (View emptyView)

设置视图以显示适配器是否为空

Parameters
emptyView View

setFocusable

Added in API level 1
void setFocusable (boolean focusable)

设置此视图是否可以获得焦点。 将其设置为false也将确保此视图在触摸模式下不可聚焦。

Parameters
focusable boolean: If true, this view can receive the focus.

setFocusableInTouchMode

Added in API level 1
void setFocusableInTouchMode (boolean focusable)

设置此视图是否可以在触摸模式下获得焦点。 将其设置为true还将确保此视图可以聚焦。

Parameters
focusable boolean: If true, this view can receive the focus while in touch mode.

setOnClickListener

Added in API level 1
void setOnClickListener (View.OnClickListener l)

单击此视图时注册要调用的回调。 如果此视图不可点击,则可点击。

Parameters
l View.OnClickListener: The callback that will run

setOnItemClickListener

Added in API level 1
void setOnItemClickListener (AdapterView.OnItemClickListener listener)

当单击此AdapterView中的某个项目时,注册要调用的回调。

Parameters
listener AdapterView.OnItemClickListener: The callback that will be invoked.

setOnItemLongClickListener

Added in API level 1
void setOnItemLongClickListener (AdapterView.OnItemLongClickListener listener)

当单击并保持此AdapterView中的某个项目时,注册要调用的回调

Parameters
listener AdapterView.OnItemLongClickListener: The callback that will run

setOnItemSelectedListener

Added in API level 1
void setOnItemSelectedListener (AdapterView.OnItemSelectedListener listener)

当选择了此AdapterView中的项目时,注册要调用的回调。

Parameters
listener AdapterView.OnItemSelectedListener: The callback that will run

setSelection

Added in API level 1
void setSelection (int position)

设置当前选择的项目。 要支持覆盖此方法的辅助功能子类,必须首先调用overriden超级方法。

Parameters
position int: Index (starting at 0) of the data item to be selected.

Protected methods

canAnimate

Added in API level 1
boolean canAnimate ()

指示视图组是否能够在第一个布局之后为其子项设置动画。

Returns
boolean true if the children can be animated, false otherwise

dispatchRestoreInstanceState

Added in API level 1
void dispatchRestoreInstanceState (SparseArray<Parcelable> container)

覆盖以防止适配器创建的任何视图解冻。

Parameters
container SparseArray: The SparseArray which holds previously saved state.

dispatchSaveInstanceState

Added in API level 1
void dispatchSaveInstanceState (SparseArray<Parcelable> container)

覆盖以防止冻结由适配器创建的任何视图。

Parameters
container SparseArray: The SparseArray in which to save the view's state.

onDetachedFromWindow

Added in API level 1
void onDetachedFromWindow ()

这是在视图从窗口分离时调用的。 此时它不再有绘图表面。

onLayout

Added in API level 1
void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。 带孩子的派生类应该覆盖这个方法,并调用他们每个孩子的布局。

Parameters
changed boolean: This is a new size or position for this view
left int: Left position, relative to parent
top int: Top position, relative to parent
right int: Right position, relative to parent
bottom int: Bottom position, relative to parent

Hooray!