您的位置:首页 > 其它

RecyclerView.LayoutManager的常用相关方法

2017-01-12 16:33 411 查看
相关的类

相关的方法

目前RecyclerView已经基本替换了原来的ListView,也许性能上还没达到我们想要的效果,但是RecyclerView可以说是更加灵活了。为了达到某些效果,你也许需要了解RecyclerView的布局管理器RecyclerView.LayoutManager的相关方法。今天,我们就来扒一扒。

1. 相关的类



由官方API文档我们可以清楚的获知RecyclerView.LayoutManager有两个直接子类LinearLayout.Manager(线性布局管理器),StaggeredGridLayoutManager(交叉网格布局管理器),一个间接子类GridLayoutManager(网格布局管理器)。

2. 相关的方法

void addView(View child, int index)

根据索引在RecyclerView中添加子View。

void addView(View child)

添加子View。

int computeHorizontalScrollExtent(RecyclerView.State state)

int computeHorizontalScrollOffset(RecyclerView.State state)

int computeHorizontalScrollRange(RecyclerView.State state)

int computeVerticalScrollExtent(RecyclerView.State state)

int computeVerticalScrollOffset(RecyclerView.State state)

int computeVerticalScrollRange(RecyclerView.State state)

官方API文档已经说的很清楚了,如果要实现scroll bar可能就需要复写以上的六个方法中的某一些。

View findViewByPosition(int position)

根据Adapter的位置查找RecyclerView的子View。

View getChildAt(int index)

根据索引获取子View。

int getChildCount()

获取RecyclerView的子View的个数。

int getHeight()

int getWidth()

获取RecyclerView的高度和宽度的两个函数

int getItemCount()

获取Adapter中子项的个数

int getItemViewType(View view)

获取Adapter 中定义的子项的ViewType(子项View的类型),在RecyclerView中并没有ListView的head和footer的实现方法,这个方法也提供了head和footer的实现思路,在初始位置返回的ViewType为head,末尾位置返回的ViewType为footer,根据返回的ViewType的不同,我们可以去创建不同的子项的View和绑定不同的ViewHolder。

void moveView(int fromIndex, int toIndex)

移动一个View的位置到另一个View。

Parcelable onSaveInstanceState()

当需要保存LayoutManager的状态时调用。

void onScrollStateChanged(int state)

当滑动状态改变时RecyclerView调用这个方法去通知LayoutManager。

void removeAllViews()

Remove all views from the currently attached RecyclerView.

移除当前依附在RecyclerView上的所有View。

void removeView(View child)

移除当前依附在RecyclerView上的一个子View。

void removeViewAt(int index)

根据索引移除当前依附在RecyclerView上的一个子View。

好了,就暂且说到这里,更多的方法和子类中的方法,小伙伴可以自行去看官方的API文档。

https://developer.android.google.cn/reference/android/support/v7/widget/RecyclerView.LayoutManager.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api