RecyclerView API译文

notifyItemChanged(int position) 更新列表position位置上的数据可以调用
notifyItemInserted(int position) 列表position位置添加一条数据时可以调用,伴有动画效果
notifyItemRemoved(int position) 列表position位置移除一条数据时调用,伴有动画效果
notifyItemMoved(int fromPosition, int toPosition) 列表fromPosition位置的数据移到toPosition位置时调用,伴有动画效果
notifyItemRangeChanged(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项进行数据刷新
notifyItemRangeInserted(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量添加数据时调用,伴有动画效果
notifyItemRangeRemoved(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量删除数据时调用,伴有动画效果

简介:

提供一个让有限的窗口变成一个大数据集的灵活视图。

术语表:

Adapter:RecyclerView的子类。负责提供在一个数据集中子item显示布局的适配器。Position:在一个适配器子item的位置。Index:用于调用getChildAt(int)时根据位置进行添加子视图的下标。Binding:在适配器中的位置显示数据的过程Recycle (view):以前用于将特定的适配器位置显示数据的视图可以放置在稍后再使用的缓存中,随后再次显示相同类型的数据。这可以大大提高性能,跳过初始布局膨胀或创建。Scrap (view):在布局中已进入一个临时分离状态的小视图。不用的界面可以重复使用不必完全从父recyclerview分离,如果觉得view是dirty,直接修改假如不需要重新绑定的,或者修改适配器。Dirty (view): 在显示之前,必须在适配器上弹回的子视图。

概要

《RecyclerView API译文》 这里写图片描述

XML 属性

layoutManager 用法跟 Layout Manager类一样
Public Methods
public void addFocusables (ArrayList views, int direction, int focusableMode)
将需要获得焦点的View添加到获得焦点视图的列表中。
Parameters views 不为空的view集合 direction 焦点的方向。 focusableMode 要添加的focusables类型。
public void addItemDecoration (RecyclerView.ItemDecoration decor)
在recyclerview添加一个recyclerview.itemdecoration。 Item decorations可以影响测量和绘制单个子视图。
Item decorations 是一个指令. 放置在列表前面的装饰物将被运行/queried/绘制首先对项目视图的影响。添加到视图的Padding将被嵌套;早期的装饰添加的填充物将意味着该列表中的进一步项目装饰将被要求在前一个装饰区内draw/pad 。
Parameters decor 添加的装饰
public void addItemDecoration (RecyclerView.ItemDecoration decor, int index)
用法同上
Parameters decor Decoration to add index 在装饰链中插入这一装饰的位置。如果这个值是负的装饰将增加在最后。
public void addOnChildAttachStateChangeListener (RecyclerView.OnChildAttachStateChangeListener listener)
注册一个监听器,将通知每当子视图连接或脱离recyclerview。
该监听器是调用LayoutManager或recyclerview判断子视图不再需要。如果一个应用程序将昂贵的或重量级的数据与项目视图关联,这可能是释放或释放这些资源的好地方。
Parameters listener Listener to register
public void addOnItemTouchListener (RecyclerView.OnItemTouchListener listener)
添加一个recyclerview.onitemtouchlistener拦截触摸事件之前他们被派遣到子视图或视图的标准滚动行为。
客户端代码可以使用监听器来实现项目操作行为。一旦一个监听器返回从onintercepttouchevent(recyclerview,位移事件)的onTouchEvent(recyclerview,位移事件)的方法将为每个输入位移事件直到姿态结束。
Parameters listener Listener to add 也可以看 RecyclerView.SimpleOnItemTouchListener
public void addOnScrollListener (RecyclerView.OnScrollListener listener)
添加一个监听器,将通知任何更改滚动状态或位置。
添加一个监听器的组件应该小心地把它删除完毕。,获取拥有者的其他的组件可以调用clearonscrolllisteners()去除所有附加的监听者。
Parameters listener listener to set or null to clear
public void clearOnChildAttachStateChangeListeners ()
移除所有通addonchildattachstatechangelistener(onchildattachstatechangelistener)添加的监听者。
public void clearOnScrollListeners ()
删除所有的其余监听器,通知任何更改滚动状态或位置。
public int computeHorizontalScrollExtent ()
计算水平滚动条上的滑块在水平范围内的横向距离。这个值是用来计算拇指长度内滚动的轨迹。
范围表示在任意的单位,必须按照computehorizontalscrollrange()和computehorizontalscrolloffset()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The horizontal extent of the scrollbar’s thumb See Also computeHorizontalScrollExtent(RecyclerView.State)
public int computeHorizontalScrollOffset ()
计算水平滚动条上的滑块在水平范围内的横向偏移。这个值是用来计算拇指长度内滚动的轨迹。
范围表示在任意的单位,必须按照computehorizontalscrollrange()和computehorizontalscrollextent()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The horizontal offset of the scrollbar’s thumb See Also (RecyclerView.Adapter)
public int computeHorizontalScrollRange ()
计算出水平滚动条的水平距离。
范围表示在任意的单位,必须按照computehorizontalscrollextent()和computehorizontalscrolloffset()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The total horizontal range represented by the vertical scrollbar
See Also computeHorizontalScrollRange(RecyclerView.State)
public int computeVerticalScrollExtent ()
计算的垂直滚动条上的滑块在垂直范围内的垂直距离。这个值是用来计算拇指长度内滚动的轨迹。
范围表示在任意的单位,必须按照computeverticalscrollrange()和computeverticalscrolloffset()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The vertical extent of the scrollbar’s thumb See Also computeVerticalScrollExtent(RecyclerView.State)
public int computeVerticalScrollOffset ()
计算的垂直滚动条上在垂直范围内的滑块垂直偏移。这个值是用来计算拇指长度内滚动的轨迹。
范围表示在任意的单位,必须按照computeverticalscrollrange()和computeverticalscrolloffset()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The vertical offset of the scrollbar’s thumb See Also (RecyclerView.Adapter)
public int computeVerticalScrollRange ()
计算的垂直距离,垂直滚动条代表。
范围表示在任意的单位,必须按照computeverticalscrollrange()和computeverticalscrolloffset()单位使用相同的。
默认实现返回0。
如果你想支持滚动条,在你的LayoutManager覆盖computehorizontalscrollextent(recyclerview.state)。
Returns The total vertical range represented by the vertical scrollbar
See Also computeVerticalScrollRange(RecyclerView.State)
public boolean dispatchNestedFling (float velocityX, float velocityY, boolean consumed)
向一个嵌套滚动父控件发送一次一次的发送。
此方法应该被用来表示一个嵌套滚动的子视图已经检测到一个一个的合适的条件。通常这意味着触摸滚动结束在滚动,满足或超过最小投掷速度沿滚动轴方向的速度。
如果一个嵌套的滚动视图通常会抛出,但它是在它自己的内容的边缘,它可以使用此方法将其委托给它的嵌套滚动的父控件。父控件可以随意地使用一次或观察一个子视图扔。
Parameters velocityX Horizontal fling velocity in pixels per second velocityY Vertical fling velocity in pixels per second consumed true if the child consumed the fling, false otherwise Returns true if the nested scrolling parent consumed or otherwise reacted to the fling
public boolean dispatchNestedPreFling (float velocityX, float velocityY)
在这个视图处理之前,将一个发送给嵌套滚动的父控件。
嵌套的预抛事件是接触和嵌套的预滚动是嵌套的事件。dispatchnestedprefling偏移条件,在子视图消耗它之前父视图完全消耗嵌套滑动。如果该方法返回true,则嵌套的父视图将被消耗掉,而该视图不应该滚动。
对于一个更好的用户体验,只有一个视图中的嵌套滚动链应该依次被消耗。如果一个父视图消耗了这个方法,该方法将返回false。自定义视图的实现应该以2种方式来解释:
如果一个自定义视图分页需要解决的一个固定的页面上,不叫dispatchnestedprefling;消费将解决一个有效的位置无关。 如果一个嵌套的父控件会消耗掉这一点,这个视图就不应该滚动,甚至可以解决一个有效的空闲位置。 视图应该也不提供抛速度嵌套父视图沿轴滚动目前不支持;ScrollView不应该提供水平扫视速度其父母从滚动沿着轴是不允许携带速度沿运动是没有道理的。
Parameters velocityX Horizontal fling velocity in pixels per second velocityY Vertical fling velocity in pixels per second Returns true if a nested scrolling parent consumed the fling
public boolean dispatchNestedPreScroll (int dx, int dy, int[] consumed, int[] offsetInWindow)
在这个视图消耗任何部分的情况之前,在进行中的嵌套滚动调度的一个步骤。
嵌套预滚动事件是拦截触摸嵌套滚动事件。dispatchnestedprescroll提供了一个机会,在子视图消耗它之前,嵌套的滚动操作的父视图消耗一些或所有的滚动操作。
Parameters dx Horizontal scroll distance in pixels dy Vertical scroll distance in pixels consumed Output. If not null, consumed[0] will contain the consumed component of dx and consumed[1] the consumed dy. offsetInWindow Optional. If not null, on return this will contain the offset in local view coordinates of this view from before this operation to after it completes. View implementations may use this to adjust expected input coordinate tracking. Returns true if the parent consumed some or all of the scroll delta
public boolean dispatchNestedScroll (int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)
一步一个嵌套滚动的进度。
支持嵌套滚动的视图的实现,应该将此调用此报告信息,以向当前嵌套滚动的父控件。如果一个嵌套滚动不是当前正在进行中的或嵌套滚动,则此方法没有启用此方法。
兼容视图实现应该也在消耗自己的滚动事件组件调用dispatchnestedprescroll。
Parameters dxConsumed Horizontal distance in pixels consumed by this view during this scroll step dyConsumed Vertical distance in pixels consumed by this view during this scroll step dxUnconsumed Horizontal scroll distance in pixels not consumed by this view dyUnconsumed Horizontal scroll distance in pixels not consumed by this view offsetInWindow Optional. If not null, on return this will contain the offset in local view coordinates of this view from before this operation to after it completes. View implementations may use this to adjust expected input coordinate tracking. Returns true if the event was dispatched, false if it could not be dispatched.
public void draw (Canvas c)
绘制view的方法
Parameters c The Canvas to which the View is rendered.
public boolean drawChild (Canvas canvas, View child, long drawingTime)
在View Group 绘制一个子view。这种方法是负责在正确的状态得到画布。这包括裁剪,将使子view的滚动的起源是在0,0,和使用任何动画转换。
Parameters canvas The canvas on which to draw the child child Who to draw drawingTime The time at which draw is occurring Returns True if an invalidate() was issued
public View findChildViewUnder (float x, float y)
给定的点下找到的最顶层视图。
Parameters x Horizontal position in pixels to search y Vertical position in pixels to search Returns The child view under (x, y) or null if no matching child is found
public RecyclerView.ViewHolder findViewHolderForAdapterPosition (int position)
返回的数据集的特定位置的项目viewholder。不像findviewholderforlayoutposition(int)这种方法考虑到任何挂起的适配器的变化可能不会反映到尚未布局。另一方面,如果notifydatasetchanged()被称为但新布局尚未计算,此方法将返回空直到从未知的布局计算views的新位置。 这种方法检查只有recyclerview的子view。如果该项目在给定的位置没有被安排,它不会创建一个新的。
Parameters position The position of the item in the data set of the adapter Returns The ViewHolder at position or null if there is no such item
public RecyclerView.ViewHolder findViewHolderForItemId (long id)
返回与给定的ID。recyclerview必须使用stableids适配器返回一个非空值的项的viewholder。
这种方法检查只有recyclerview的子view。如果该项目在给定的位置没有被安排,它不会创建一个新的。
Parameters id The id for the requested item Returns The ViewHolder with the given id or null if there is no such item
public RecyclerView.ViewHolder findViewHolderForLayoutPosition (int position)
返回在设定的新布局,通过数据的给定位置的项目viewholder。
这种方法检查只有recyclerview的子view。如果该项目在给定的位置没有被安排,它不会创建一个新的。
注意,当适配器的内容改变,viewholder位置直到下一步布局计算更新。如果有挂起的适配器更新,该方法的返回值可能无法匹配您的适配器内容。你可以使用# getadapterposition()得到一个viewholder当前适配器的位置。
Parameters position The position of the item in the data set of the adapter Returns The ViewHolder at position or null if there is no such item
public RecyclerView.ViewHolder findViewHolderForPosition (int position)
This method is deprecated. use findViewHolderForLayoutPosition(int) or findViewHolderForAdapterPosition(int)
public boolean fling (int velocityX, int velocityY)
Begin a standard fling with an initial velocity along each axis in pixels per second. If the velocity given is below the system-defined minimum this method will return false and no fling will occur.
Parameters velocityX Initial horizontal velocity in pixels per second velocityY Initial vertical velocity in pixels per second Returns true if the fling was started, false if the velocity was too low to fling or LayoutManager does not support scrolling in the axis fling is issued. See Also canScrollVertically() canScrollHorizontally()
public View focusSearch (View focused, int direction)
Find the nearest view in the specified direction that wants to take focus.
Parameters focused The view that currently has focus direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT, or 0 for not applicable.
public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
Parameters attrs the attributes to build the layout parameters from Returns an instance of ViewGroup.LayoutParams or one of its descendants
public Adapter getAdapter ()
Retrieves the previously set adapter or null if no adapter is set.
Returns The previously set adapter See Also setAdapter(Adapter)
public int getBaseline ()
返回从其顶部边界的recyclerview文本基线偏移。如果这recyclerview的布局管理器不支持基线对齐,此方法返回-1。
Returns the offset of the baseline within the RecyclerView’s bounds or -1 if baseline alignment is not supported
public int getChildAdapterPosition (View child)
Return the adapter position that the given child view corresponds to.
Parameters child Child View to query Returns Adapter position corresponding to the given view or NO_POSITION
public long getChildItemId (View child)
Return the stable item id that the given child view corresponds to.
Parameters child Child View to query Returns Item id corresponding to the given view or NO_ID
public int getChildLayoutPosition (View child)
Return the adapter position of the given child view as of the latest completed layout pass.
This position may not be equal to Item’s adapter position if there are pending changes in the adapter which have not been reflected to the layout yet.
Parameters child Child View to query Returns Adapter position of the given View as of last layout pass or NO_POSITION if the View is representing a removed item.
public RecyclerView.ViewHolder getChildViewHolder (View child)
Retrieve the RecyclerView.ViewHolder for the given child view.
Parameters child Child of this RecyclerView to query for its ViewHolder Returns The child view’s ViewHolder
public RecyclerViewAccessibilityDelegate getCompatAccessibilityDelegate ()
返回可委托实施的recyclerview使用兼容性。
Returns An instance of AccessibilityDelegateCompat used by RecyclerView
public RecyclerView.ItemAnimator getItemAnimator ()
获取当前itemanimator这recyclerview。一个空的返回值表示没有动画,项目将没有任何动画发生变化。默认情况下,recyclerview实例化和使用defaultitemanimator实例。
Returns ItemAnimator The current ItemAnimator. If null, no animations will occur when changes occur to the items in this RecyclerView.
public RecyclerView.LayoutManager getLayoutManager ()
返回当前负责这recyclerview布局方式的recyclerview.layoutmanager。
Returns The currently bound LayoutManager
public int getMaxFlingVelocity ()
返回的recyclerview用最大滑动速度.
Returns The maximum fling velocity used by this RecyclerView.
public int getMinFlingVelocity ()
返回开始一次滑动的最低速度。
Returns The minimum velocity to start a fling
public RecyclerView.RecycledViewPool getRecycledViewPool ()
检索此recyclerview的recyclerview.recycledviewpool。这个方法将永远不会返回空;如果没有池是这个视图,将创建一个新的。更多信息见setrecycledviewpool。
Returns The pool used to store recycled item views for reuse. See Also setRecycledViewPool(RecycledViewPool)
public int getScrollState ()
返回的recyclerview当前滚动状态。
Returns SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING or SCROLL_STATE_SETTLING public boolean hasFixedSize ()
Returns true if the app has specified that changes in adapter content cannot change the size of the RecyclerView itself.
public boolean hasNestedScrollingParent ()
如果这个视图有一个嵌套滚动的父控件则返回true。
嵌套滚动父控件的存在表明,该视图已开始进行嵌套滚动,并且被一个父视图接受进一步的视图层次结构。 Returns whether this view has a nested scrolling parent
public boolean hasPendingAdapterUpdates ()
返回是否有尚未应用到布局的尚未挂起的适配器更新。
如果这个方法返回true,它意味着用户当前看到的可能不反映他们适配器的内容(取决于已更改的内容)。您可以使用此信息来推迟或取消某些操作。
此方法返回true,如果recyclerview尚未计算第一个布局后,它是连接到窗口或适配器已被取代。
Returns True if there are some adapter updates which are not yet reflected to layout or false if layout is up to date.
public void invalidateItemDecorations ()
所有itemdecorations无效。如果recyclerview有物品装饰,调用此方法将引发requestlayout()回调。
public boolean isAnimating ()
如果recyclerview当前正在运行的一些动画返回true。
如果你想被通知当动画完成后,使用时(itemanimator。itemanimatorfinishedlistener)。
Returns True if there are some item animations currently running or waiting to be started.
public boolean isAttachedToWindow ()
Returns true if RecyclerView is attached to window.
public boolean isNestedScrollingEnabled ()
Returns true if nested scrolling is enabled for this view.
If nested scrolling is enabled and this View class implementation supports it, this view will act as a nested scrolling child view when applicable, forwarding data about the scroll operation in progress to a compatible and cooperating nested scrolling parent.
Returns true if nested scrolling is enabled
public void offsetChildrenHorizontal (int dx)
通过DX像素偏移的所有子视图的边界。用于实现简单的滚动layoutmanagers。
Parameters dx Horizontal pixel offset to apply to the bounds of all child views
public void offsetChildrenVertical (int dy)
Offset the bounds of all child views by dy pixels. Useful for implementing simple scrolling in LayoutManagers.
Parameters dy Vertical pixel offset to apply to the bounds of all child views
public void onChildAttachedToWindow (View child)
Called when an item view is attached to this RecyclerView.
Subclasses of RecyclerView may want to perform extra bookkeeping or modifications of child views as they become attached. This will be called before a RecyclerView.LayoutManager measures or lays out the view and is a good time to perform these changes.
Parameters child Child view that is now attached to this RecyclerView and its associated window public void onChildDetachedFromWindow (View child)
Called when an item view is detached from this RecyclerView.
Subclasses of RecyclerView may want to perform extra bookkeeping or modifications of child views as they become detached. This will be called as a RecyclerView.LayoutManager fully detaches the child view from the parent and its window.
Parameters child Child view that is now detached from this RecyclerView and its associated window
public void onDraw (Canvas c)
Implement this to do your drawing.
Parameters c the canvas on which the background will be drawn
public boolean onGenericMotionEvent (MotionEvent event)
实现这一方法来处理一般的动作事件。 一般的动作事件描述的操纵杆移动,鼠标,触控板接触,滚轮运动和其他的输入事件。动作事件的源指定接收的输入的类别。此方法的实现必须在处理事件的源代码前检查位。下面的代码示例演示了如何做。 一般的动作事件源类source_class_pointer传递到指针下的视图。所有其他通用的运动事件被传递到聚焦视图。
public boolean onGenericMotionEvent(MotionEvent event) { if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) { if (event.getAction() == MotionEvent.ACTION_MOVE) { // process the joystick movement… return true; } } if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) { switch (event.getAction()) { case MotionEvent.ACTION_HOVER_MOVE: // process the mouse hover movement… return true; case MotionEvent.ACTION_SCROLL: // process the scroll wheel movement… return true; } } return super.onGenericMotionEvent(event); }
Parameters event The generic motion event being processed. Returns True if the event was handled, false otherwise.
public boolean onInterceptTouchEvent (MotionEvent e)
public void onScrollStateChanged (int state)
这recyclerview滚动时的状态变化。子类应该使用此方法来响应状态变化,而不是一个显式的监听器。 这种方法总是在监听者之前被调用,但运行在布局管理器响应的滚动状态的变化之后。
Parameters state the new scroll state, one of SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING or SCROLL_STATE_SETTLING
public void onScrolled (int dx, int dy)
recyclerview改变滚动条的位置回调此方法。子类应该使用此方法来响应适配器的数据集内的滚动,而不是一个显式的监听器。 这种方法监听者之前被调用。但如果在监听者运行之后子类需要在滚动执行任何额外的维护或记录,这是一个好地方。 这不同于onscrollchanged(int,int,int,int),接收距离滚动在任一方向在适配器的数据集而不是绝对滚动坐标。因为recyclerview无法计算绝对滚动位置从数据集合中的任意点,onscrollchanged总是会得到当前getscrollx()和getscrolly()值不符合设置滚动位置的数据。然而,一些子类可以选择使用这些字段作为特殊的偏移量。
Parameters dx horizontal distance scrolled in pixels dy vertical distance scrolled in pixels
public boolean onTouchEvent (MotionEvent e)
public void removeItemDecoration (RecyclerView.ItemDecoration decor)
Remove an RecyclerView.ItemDecoration from this RecyclerView.
The given decoration will no longer impact the measurement and drawing of item views.
Parameters decor Decoration to remove See Also addItemDecoration(ItemDecoration)
public void removeOnChildAttachStateChangeListener (RecyclerView.OnChildAttachStateChangeListener listener)
Removes the provided listener from child attached state listeners list.
Parameters listener Listener to unregister
public void removeOnItemTouchListener (RecyclerView.OnItemTouchListener listener)
Remove an RecyclerView.OnItemTouchListener. It will no longer be able to intercept touch events.
Parameters listener Listener to remove
public void removeOnScrollListener (RecyclerView.OnScrollListener listener)
Remove a listener that was notified of any changes in scroll state or position.
Parameters listener listener to set or null to clear
public void requestChildFocus (View child, View focused)
Called when a child of this parent wants focus
Parameters child The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus. focused The view that is a descendant of child that actually has focus
public 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. ViewGroups overriding this can trust that:
child will be a direct child of this group rectangle will be in the child’s coordinates ViewGroups overriding this should uphold the contract:
nothing will change if the rectangle is already visible the view port will be scrolled only just enough to make the rectangle visible
Parameters child The direct child making the request. rect The rectangle in the child’s coordinates the child wishes to be on the screen. immediate True to forbid animated or delayed scrolling, false otherwise Returns Whether the group scrolled to handle the operation
public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)
Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).
This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.
Parameters disallowIntercept True if the child does not want the parent to intercept touch events. public void requestLayout ()
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.
Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.
public void scrollBy (int x, int y)
Move the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.
Parameters x the amount of pixels to scroll by horizontally y the amount of pixels to scroll by vertically
public void scrollTo (int x, int y)
Set the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.
Parameters x the x position to scroll to y the y position to scroll to
public void scrollToPosition (int position)
Convenience method to scroll to a certain position. RecyclerView does not implement scrolling logic, rather forwards the call to scrollToPosition(int)
Parameters position Scroll to this adapter position See Also scrollToPosition(int)
public void sendAccessibilityEventUnchecked (AccessibilityEvent event)
This method behaves exactly as sendAccessibilityEvent(int) but takes as an argument an empty AccessibilityEvent and does not perform a check whether accessibility is enabled.
If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its sendAccessibilityEventUnchecked(View, AccessibilityEvent) is responsible for handling this call.
Parameters event The event to send.
public void setAccessibilityDelegateCompat (RecyclerViewAccessibilityDelegate accessibilityDelegate)
设置无障碍兼容使用的recyclerview通用性实现。
Parameters accessibilityDelegate The accessibility delegate to be used by RecyclerView.
public void setAdapter (Adapter adapter)
Set a new adapter to provide child views on demand.
When adapter is changed, all existing views are recycled back to the pool. If the pool has only one adapter, it will be cleared.
Parameters adapter The new adapter to set, or null to set no adapter. See Also swapAdapter(Adapter, boolean)
public void setChildDrawingOrderCallback (RecyclerView.ChildDrawingOrderCallback childDrawingOrderCallback)
Sets the RecyclerView.ChildDrawingOrderCallback to be used for drawing children.
See getChildDrawingOrder(int, int) for details. Calling this method will always call setChildrenDrawingOrderEnabled(boolean). The parameter will be true if childDrawingOrderCallback is not null, false otherwise.
Note that child drawing order may be overridden by View’s elevation.
Parameters childDrawingOrderCallback The ChildDrawingOrderCallback to be used by the drawing system.
public void setClipToPadding (boolean clipToPadding)
Sets whether this ViewGroup will clip its children to its padding, if padding is present.
By default, children are clipped to the padding of their parent Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Parameters clipToPadding true to clip children to the padding of the group, false otherwise
public void setHasFixedSize (boolean hasFixedSize)
RecyclerView can perform several optimizations if it can know in advance that changes in adapter content cannot change the size of the RecyclerView itself. If your use of RecyclerView falls into this category, set this to true.
Parameters hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.
public void setItemAnimator (RecyclerView.ItemAnimator animator)
设置recyclerview.itemanimator将处理涉及的变化在这recyclerview子view动画。默认情况下,recyclerview实例化和使用defaultitemanimator实例。无论项目动画的recyclerview启用取决于itemanimator是否支持项目动画的布局管理器。
Parameters animator The ItemAnimator being set. If null, no animations will occur when changes occur to the items in this RecyclerView.
public void setItemViewCacheSize (int size)
设置在添加他们的潜在共享再生view池之前保留的屏幕视图大小。
屏幕视图缓存保持意识到连接适配器的变化,让LayoutManager重复使用未经修改而不需要返回到适配器绑定他们的view。
Parameters size Number of views to cache offscreen before returning them to the general recycled view pool
public void setLayoutManager (RecyclerView.LayoutManager layout)
设置这recyclerview将使用的recyclerview.layoutmanager。
对比其他适配器支持的观点如ListView和GridView,recyclerview允许客户端代码为子视图的自定义布局安排。这些安排是由recyclerview.layoutmanager控制。一个布局管理器必须提供recyclerview功能。
几个默认策略是为常用的使用,如列表和网格。
Parameters layout LayoutManager to use
public void setNestedScrollingEnabled (boolean enabled)
启用或禁用此视图嵌套滚动。
如果将此属性设置为true,则可允许在当前层次结构中允许启动嵌套滚动操作以兼容父视图。如果这个视图没有实现嵌套滚动,这将不会有效果。禁用嵌套滚动,而嵌套滚动正在进行中,有停止嵌套滚动的效果。
Parameters enabled true to enable nested scrolling, false to disable
public void setRecycledViewPool (RecyclerView.RecycledViewPool pool)
Recycled view pools允许多个recyclerviews分享不用的view公共池。可如果你有相同的view,使用适配器类型多recyclerviews是有用的,例如,如果你在项目视图的显示同一种ViewPager有几个数据集。
Parameters pool Pool to set. If this parameter is null a new pool will be created and used.
public void setRecyclerListener (RecyclerView.RecyclerListener listener)
注册一个监听器,当子view被回收时,将得到通知。 当LayoutManager或recyclerview决定子视图不再需要时该监听者将被调用。如果一个应用程序将昂贵的或重量级的数据与项目视图关联,这可能是释放或释放这些资源的好地方。
Parameters listener Listener to register, or null to clear
public void setScrollingTouchSlop (int slopConstant)
Configure the scrolling touch slop for a specific use case. Set up the RecyclerView’s scrolling motion threshold based on common usages. Valid arguments are TOUCH_SLOP_DEFAULT and TOUCH_SLOP_PAGING.
Parameters slopConstant One of the TOUCH_SLOP_ constants representing the intended usage of this RecyclerView
public void setViewCacheExtension (RecyclerView.ViewCacheExtension extension)
设置回收器将被一种新的recyclerview.viewcacheextension使用。
Parameters extension ViewCacheExtension to be used or null if you want to clear the existing one. See Also ERROR(ViewCacheExtension#getViewForPositionAndType(Recycler, int, int)} /{@link ViewCacheExtension#getViewForPositionAndType(Recycler, int, int)})
public void smoothScrollBy (int dx, int dy)
由给定的像素沿任一轴滚动滚动。
Parameters dx Pixels to scroll horizontally dy Pixels to scroll vertically
public void smoothScrollToPosition (int position)
开始一个平滑滚动到适配器指定位置。
支持平滑滚动,你必须重写smoothscrolltoposition(recyclerview,状态,int)和创建一个recyclerview.smoothscroller。 recyclerview.layoutmanager负责创建实际的滚动动作。如果你想提供一个自定义平滑滚动逻辑,重写smoothscrolltoposition(recyclerview,状态,int)在你的布局管理器。
Parameters position The adapter position to scroll to See Also smoothScrollToPosition(RecyclerView, State, int)
public boolean startNestedScroll (int axes)
Begin a nestable scroll operation along the given axes.
A view starting a nested scroll promises to abide by the following contract:
The view will call startNestedScroll upon initiating a scroll operation. In the case of a touch scroll this corresponds to the initial ACTION_DOWN. In the case of touch scrolling the nested scroll will be terminated automatically in the same manner as requestDisallowInterceptTouchEvent(boolean). In the event of programmatic scrolling the caller must explicitly call stopNestedScroll() to indicate the end of the nested scroll.
If startNestedScroll returns true, a cooperative parent was found. If it returns false the caller may ignore the rest of this contract until the next scroll. Calling startNestedScroll while a nested scroll is already in progress will return true.
At each incremental step of the scroll the caller should invoke dispatchNestedPreScroll once it has calculated the requested scrolling delta. If it returns true the nested scrolling parent at least partially consumed the scroll and the caller should adjust the amount it scrolls by.
After applying the remainder of the scroll delta the caller should invoke dispatchNestedScroll, passing both the delta consumed and the delta unconsumed. A nested scrolling parent may treat these values differently. See onNestedScroll(View, int, int, int, int).
Parameters axes Flags consisting of a combination of SCROLL_AXIS_HORIZONTAL and/or SCROLL_AXIS_VERTICAL. Returns true if a cooperative parent was found and nested scrolling has been enabled for the current gesture.
public void stopNestedScroll ()
Stop a nested scroll in progress.
Calling this method when a nested scroll is not currently in progress is harmless.
public void stopScroll ()
Stop any current scroll in progress, such as one started by smoothScrollBy(int, int), fling(int, int) or a touch-initiated fling.
public void swapAdapter (Adapter adapter, boolean removeAndRecycleExistingViews)
提供一个新适配器替换当前适配器。它类似于setadapter(适配器)但假设现有的适配器和新的适配器使用相同的recyclerview.viewholder和不明确的recycledviewpool。 注意,还需要onadapterchanged回调。
Parameters adapter The new adapter to set, or null to set no adapter. removeAndRecycleExistingViews If set to true, RecyclerView will recycle all existing Views. If adapters have stable ids and/or you want to animate the disappearing views, you may prefer to set this to false. See Also setAdapter(Adapter)

    原文作者:酷泡泡
    原文地址: https://www.jianshu.com/p/4164871613ed
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞