1. 程式人生 > >android官方API之RecyclerView

android官方API之RecyclerView

來源:https://developer.android.com/reference/android/support/v7/widget/RecyclerView

RecyclerView

public class RecyclerView 
extends ViewGroup implements ScrollingViewNestedScrollingChild2

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.v7.widget.RecyclerView
Known Direct Subclasses

BaseGridView,WearableRecyclerView

Known Indirect Subclasses

HorizontalGridView

,VerticalGridView

 


A flexible view for providing a limited window into a large data set.

Glossary of terms:

  • Adapter: A subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set.
  • Position: The position of a data item within an Adapter
    .
  • Index: The index of an attached child view as used in a call to getChildAt(int). Contrast with Position.
  • Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
  • Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
  • Scrap (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
  • Dirty (view): A child view that must be rebound by the adapter before being displayed.

Positions in RecyclerView:

RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter andRecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation. This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.

For this reason, there are two types of position related methods in RecyclerView:

  • layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager's perspective.
  • adapter position: Position of an item in the adapter. This is the position from the Adapter's perspective.

These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.

Methods that return or receive *LayoutPosition* use position as of the latest layout calculation (e.g. getLayoutPosition()findViewHolderForLayoutPosition(int)). These positions include all changes until the last layout calculation. You can rely on these positions to be consistent with what user is currently seeing on the screen. For example, if you have a list of items on the screen and user asks for the 5th element, you should use these methods as they'll match what user is seeing.

The other set of position related methods are in the form of *AdapterPosition*. (e.g. getAdapterPosition(),findViewHolderForAdapterPosition(int)) You should use these methods when you need to work with up-to-date adapter positions even if they may not have been reflected to layout yet. For example, if you want to access the item in the adapter on a ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged() has been called and new layout has not yet been calculated. For this reasons, you should carefully handle NO_POSITION or null results from these methods.

When writing a RecyclerView.LayoutManager you almost always want to use layout positions whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions.

Summary

Nested classes

class RecyclerView.Adapter<VH extends RecyclerView.ViewHolder>

Base class for an Adapter

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView

class RecyclerView.AdapterDataObserver

Observer base class for watching changes to an RecyclerView.Adapter

interface RecyclerView.ChildDrawingOrderCallback

A callback interface that can be used to alter the drawing order of RecyclerView children. 

class RecyclerView.EdgeEffectFactory

EdgeEffectFactory lets you customize the over-scroll edge effect for RecyclerViews. 

class RecyclerView.ItemAnimator

This class defines the animations that take place on items as changes are made to the adapter. 

class RecyclerView.ItemDecoration

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. 

class RecyclerView.LayoutManager

LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. 

class RecyclerView.LayoutParams

LayoutParams subclass for children of RecyclerView

interface RecyclerView.OnChildAttachStateChangeListener

A Listener interface that can be attached to a RecylcerView to get notified whenever a ViewHolder is attached to or detached from RecyclerView. 

class RecyclerView.OnFlingListener

This class defines the behavior of fling if the developer wishes to handle it. 

interface RecyclerView.OnItemTouchListener

An OnItemTouchListener allows the application to intercept touch events in progress at the view hierarchy level of the RecyclerView before those touch events are considered for RecyclerView's own scrolling behavior. 

class RecyclerView.OnScrollListener

An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView. 

class RecyclerView.RecycledViewPool

RecycledViewPool lets you share Views between multiple RecyclerViews. 

class RecyclerView.Recycler

A Recycler is responsible for managing scrapped or detached item views for reuse. 

interface RecyclerView.RecyclerListener

A RecyclerListener can be set on a RecyclerView to receive messages whenever a view is recycled. 

class RecyclerView.SimpleOnItemTouchListener

An implementation of RecyclerView.OnItemTouchListener that has empty method bodies and default return values. 

class RecyclerView.SmoothScroller

 

Base class for smooth scrolling. 

class RecyclerView.State

 

Contains useful information about the current RecyclerView state like target scroll position or view focus. 

class RecyclerView.ViewCacheExtension

ViewCacheExtension is a helper class to provide an additional layer of view caching that can be controlled by the developer. 

class RecyclerView.ViewHolder

A ViewHolder describes an item view and metadata about its place within the RecyclerView. 

XML attributes

RecyclerView_layoutManager  

Constants

int HORIZONTAL

 

int INVALID_TYPE

 

long NO_ID

 

int NO_POSITION

 

int SCROLL_STATE_DRAGGING

The RecyclerView is currently being dragged by outside input such as user touch input.

int SCROLL_STATE_IDLE

The RecyclerView is not currently scrolling.

int SCROLL_STATE_SETTLING

The RecyclerView is currently animating to a final position while not under outside control.

int TOUCH_SLOP_DEFAULT

Constant for use with setScrollingTouchSlop(int).

int TOUCH_SLOP_PAGING

Constant for use with setScrollingTouchSlop(int).

int VERTICAL

 

Inherited constants

From class android.view.ViewGroup
int CLIP_TO_PADDING_MASK

 

int FOCUS_AFTER_DESCENDANTS

 

int FOCUS_BEFORE_DESCENDANTS

 

int FOCUS_BLOCK_DESCENDANTS

 

int LAYOUT_MODE_CLIP_BOUNDS

 

int LAYOUT_MODE_OPTICAL_BOUNDS

 

int PERSISTENT_ALL_CACHES

 

int PERSISTENT_ANIMATION_CACHE

 

int PERSISTENT_NO_CACHE

 

int PERSISTENT_SCROLLING_CACHE

 

From class android.view.View
int ACCESSIBILITY_LIVE_REGION_ASSERTIVE

 

int ACCESSIBILITY_LIVE_REGION_NONE

 

int ACCESSIBILITY_LIVE_REGION_POLITE

 

int AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS

 

String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE

 

String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY

 

String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH

 

String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR

 

String AUTOFILL_HINT_CREDIT_CARD_NUMBER

 

String AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE

 

String AUTOFILL_HINT_EMAIL_ADDRESS

 

String AUTOFILL_HINT_NAME

 

String AUTOFILL_HINT_PASSWORD

 

String AUTOFILL_HINT_PHONE

 

String AUTOFILL_HINT_POSTAL_ADDRESS

 

String AUTOFILL_HINT_POSTAL_CODE

 

String AUTOFILL_HINT_USERNAME

 

int AUTOFILL_TYPE_DATE

 

int AUTOFILL_TYPE_LIST

 

int AUTOFILL_TYPE_NONE

 

int AUTOFILL_TYPE_TEXT

 

int AUTOFILL_TYPE_TOGGLE

 

int DRAG_FLAG_GLOBAL

 

int DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION

 

int DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION

 

int DRAG_FLAG_GLOBAL_URI_READ

 

int DRAG_FLAG_GLOBAL_URI_WRITE

 

int DRAG_FLAG_OPAQUE

 

int DRAWING_CACHE_QUALITY_AUTO

 

int DRAWING_CACHE_QUALITY_HIGH

 

int DRAWING_CACHE_QUALITY_LOW

 

int FIND_VIEWS_WITH_CONTENT_DESCRIPTION

 

int FIND_VIEWS_WITH_TEXT

 

int FOCUSABLE

 

int FOCUSABLES_ALL

 

int FOCUSABLES_TOUCH_MODE

 

int FOCUSABLE_AUTO

 

int FOCUS_BACKWARD

 

int FOCUS_DOWN

 

int FOCUS_FORWARD

 

int FOCUS_LEFT

 

int FOCUS_RIGHT

 

int FOCUS_UP

 

int GONE

 

int HAPTIC_FEEDBACK_ENABLED

 

int IMPORTANT_FOR_ACCESSIBILITY_AUTO

 

int IMPORTANT_FOR_ACCESSIBILITY_NO

 

int IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS

 

int IMPORTANT_FOR_ACCESSIBILITY_YES

 

int IMPORTANT_FOR_AUTOFILL_AUTO

 

int IMPORTANT_FOR_AUTOFILL_NO

 

int IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS

 

int IMPORTANT_FOR_AUTOFILL_YES

 

int IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS

 

int INVISIBLE

 

int KEEP_SCREEN_ON

 

int LAYER_TYPE_HARDWARE

 

int LAYER_TYPE_NONE

 

int LAYER_TYPE_SOFTWARE

 

int LAYOUT_DIRECTION_INHERIT

 

int LAYOUT_DIRECTION_LOCALE

 

int LAYOUT_DIRECTION_LTR

 

int LAYOUT_DIRECTION_RTL

 

int MEASURED_HEIGHT_STATE_SHIFT

 

int MEASURED_SIZE_MASK

 

int MEASURED_STATE_MASK

 

int MEASURED_STATE_TOO_SMALL

 

int NOT_FOCUSABLE

 

int NO_ID

 

int OVER_SCROLL_ALWAYS

 

int OVER_SCROLL_IF_CONTENT_SCROLLS

 

int OVER_SCROLL_NEVER

 

int SCREEN_STATE_OFF

 

int SCREEN_STATE_ON

 

int SCROLLBARS_INSIDE_INSET

 

int SCROLLBARS_INSIDE_OVERLAY

 

int SCROLLBARS_OUTSIDE_INSET

 

int SCROLLBARS_OUTSIDE_OVERLAY

 

int SCROLLBAR_POSITION_DEFAULT

 

int SCROLLBAR_POSITION_LEFT

 

int SCROLLBAR_POSITION_RIGHT

 

int SCROLL_AXIS_HORIZONTAL

 

int SCROLL_AXIS_NONE

 

int SCROLL_AXIS_VERTICAL

 

int SCROLL_INDICATOR_BOTTOM

 

int SCROLL_INDICATOR_END

 

int SCROLL_INDICATOR_LEFT

 

int SCROLL_INDICATOR_RIGHT

 

int SCROLL_INDICATOR_START

 

int SCROLL_INDICATOR_TOP

 

int SOUND_EFFECTS_ENABLED

 

int STATUS_BAR_HIDDEN

 

int STATUS_BAR_VISIBLE

 

int SYSTEM_UI_FLAG_FULLSCREEN

 

int SYSTEM_UI_FLAG_HIDE_NAVIGATION

 

int SYSTEM_UI_FLAG_IMMERSIVE

 

int SYSTEM_UI_FLAG_IMMERSIVE_STICKY

 

int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

 

int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

 

int SYSTEM_UI_FLAG_LAYOUT_STABLE

 

int SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR

 

int SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

 

int SYSTEM_UI_FLAG_LOW_PROFILE

 

int SYSTEM_UI_FLAG_VISIBLE

 

int SYSTEM_UI_LAYOUT_FLAGS

 

int TEXT_ALIGNMENT_CENTER

 

int TEXT_ALIGNMENT_GRAVITY

 

int TEXT_ALIGNMENT_INHERIT

 

int TEXT_ALIGNMENT_TEXT_END

 

int TEXT_ALIGNMENT_TEXT_START

 

int TEXT_ALIGNMENT_VIEW_END

 

int TEXT_ALIGNMENT_VIEW_START

 

int TEXT_DIRECTION_ANY_RTL

 

int TEXT_DIRECTION_FIRST_STRONG

 

int TEXT_DIRECTION_FIRST_STRONG_LTR

 

int TEXT_DIRECTION_FIRST_STRONG_RTL

 

int TEXT_DIRECTION_INHERIT

 

int TEXT_DIRECTION_LOCALE

 

int TEXT_DIRECTION_LTR

 

int TEXT_DIRECTION_RTL

 

String VIEW_LOG_TAG

 

int VISIBLE

 

Inherited fields

From class android.view.View
public static final Property<View, Float> ALPHA

 

protected static final int[] EMPTY_STATE_SET

 

protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET

 

protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] ENABLED_FOCUSED_STATE_SET

 

protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] ENABLED_SELECTED_STATE_SET

 

protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] ENABLED_STATE_SET

 

protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] FOCUSED_SELECTED_STATE_SET

 

protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] FOCUSED_STATE_SET

 

protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_STATE_SET

 

protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET

 

protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_SELECTED_STATE_SET

 

protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET

 

protected static final int[] PRESSED_STATE_SET

 

protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET

 

public static final Property<View, Float> ROTATION

 

public static final Property<View, Float> ROTATION_X

 

public static final Property<View, Float> ROTATION_Y

 

public static final Property<View, Float> SCALE_X

 

public static final Property<View, Float> SCALE_Y

 

protected static final int[] SELECTED_STATE_SET

 

protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET

 

public static final Property<View, Float> TRANSLATION_X

 

public static final Property<View, Float> TRANSLATION_Y

 

public static final Property<View, Float> TRANSLATION_Z

 

protected static final int[] WINDOW_FOCUSED_STATE_SET

 

public static final Property<View, Float> X

 

public static final Property<View, Float> Y

 

public static final Property<View, Float> Z

 

Public constructors

RecyclerView(Context context)
RecyclerView(Context context, AttributeSet attrs)
RecyclerView(Context context, AttributeSet attrs, int defStyle)

Public methods

void addFocusables(ArrayList<View> views, int direction, int focusableMode)
void addItemDecoration(RecyclerView.ItemDecoration decor)

Add an RecyclerView.ItemDecoration to this RecyclerView.

void addItemDecoration(RecyclerView.ItemDecoration decor, int index)

Add an RecyclerView.ItemDecoration to this RecyclerView.

void addOnChildAttachStateChangeListener(RecyclerView.OnChildAttachStateChangeListener listener)

Register a listener that will be notified whenever a child view is attached to or detached from RecyclerView.

void addOnItemTouchListener(RecyclerView.OnItemTouchListener listener)

Add an RecyclerView.OnItemTouchListener to intercept touch events before they are dispatched to child views or this view's standard scrolling behavior.

void addOnScrollListener(RecyclerView.OnScrollListener listener)

Add a listener that will be notified of any changes in scroll state or position.

void clearOnChildAttachStateChangeListeners()

Removes all listeners that were added viaaddOnChildAttachStateChangeListener(OnChildAttachStateChangeListener).

void clearOnScrollListeners()

Remove all secondary listener that were notified of any changes in scroll state or position.

int computeHorizontalScrollExtent()

 

Compute the horizontal extent of the horizontal scrollbar's thumb within the horizontal range.

int computeHorizontalScrollOffset()

 

Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal range.

int computeHorizontalScrollRange()

 

Compute the horizontal range that the horizontal scrollbar represents.

int computeVerticalScrollExtent()

 

Compute the vertical extent of the vertical scrollbar's thumb within the vertical range.

int computeVerticalScrollOffset()

 

Compute the vertical offset of the vertical scrollbar's thumb within the vertical range.

int computeVerticalScrollRange()

 

Compute the vertical range that the vertical scrollbar represents.

boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean dispatchNestedPreFling(float velocityX, float velocityY)
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow)
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow, int type)

Dispatch one step of a nested scroll in progress before this view consumes any portion of it.

boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow, int type)

Dispatch one step of a nested scroll in progress.

boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)
void draw(Canvas c)
boolean drawChild(Canvas canvas, View child, long drawingTime)
View findChildViewUnder(float x, float y)

Find the topmost view under the given point.

View findContainingItemView(View view)

Traverses the ancestors of the given view and returns the item view that contains it and also a direct child of the RecyclerView.

RecyclerView.ViewHolder findContainingViewHolder(View view)

Returns the ViewHolder that contains the given view.

RecyclerView.ViewHolder findViewHolderForAdapterPosition(int position)

Return the ViewHolder for the item in the given position of the data set.

RecyclerView.ViewHolder findViewHolderForItemId(long id)

Return the ViewHolder for the item with the given id.

RecyclerView.ViewHolder findViewHolderForLayoutPosition(int position)

Return the ViewHolder for the item in the given position of the data set as of the latest layout pass.

RecyclerView.ViewHolder findViewHolderForPosition(int position)

This method was deprecated in API level 22.1.0. use findViewHolderForLayoutPosition(int)or findViewHolderForAdapterPosition(int)

boolean fling(int velocityX, int velocityY)

Begin a standard fling with an initial velocity along each axis in pixels per second.

View focusSearch(View focused, int direction)

Since RecyclerView is a collection ViewGroup that includes virtual children (items that are in the Adapter but not visible in the UI), it employs a more involved focus search strat