=================================================================================
与自定义ViewGroup
类似,自定义LayoutManager
所要做的就是ItemView
的「添加(add)」、「测量(measure)」、「布局(layout)」。
但是与自定义ViewGroup
相比,LayoutManager
多了一个「回收(recycle)」工作。
在自定义LayoutManager
之前,需要对其提供的「测量」、「布局」以及「回收」相关的API进行了解。
首先介绍测量方法,与自定义ViewGroup类似,测量通常是固定的逻辑不需要自己实现,开发者无需复写测量方法,只需要在布局之前调用测量函数来获取将要布局的「View的宽度」即可。
LayoutManager
提供了两个用来测量子View的方法:
//测量子View
public void measureChild(@NonNull View child, int widthUsed, int heightUsed)
//测量子View,并将子View的Margin也考虑进来,通常使用此函数
public void measureChildWithMargins(@NonNull View child, int widthUsed, int heightUsed)
测量完成后,便可以使用getMeasuredWidth()
、getMeasuredHeight()
直接获取View的宽高,但是在自定义LayoutManager
中需要考虑ItemDecoration
,所以需要通过如下两个API获取测量后的View大小:
//获取child的宽度,并将ItemDecoration考虑进来
public int getDecoratedMeasuredWidth(@NonNull View child) {
final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
return child.getMeasuredWidth() + insets.left + insets.right;
}
//获取child的高度,并将ItemDecoration考虑进来
public int getDecoratedMeasuredHeight(@NonNull View child) {
final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
return child.getMeasuredHeight() + insets.top + insets.bottom;
}
然后介绍layout
方法,和自定义ViewGroup
一样,LayoutManager
完成ItemView
的测量后就是布局了。
在LayoutManager
中,并非靠直接调用ItemView的layout
函数进行子View的布局,而是使用layoutDecorated
与layoutDecoratedWithMargins
, 两者的区别是后者考虑了Margins:
public void layoutDecorated(@NonNull View child, int left, int top, int right, int bottom) {
final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets;
child.layout(left + insets.left, top + insets.top, right - insets.right,
bottom - insets.bottom);
}
public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, int right,
int bottom) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final Rect insets = lp.mDecorInsets;
child.layout(left + insets.left + lp.leftMargin, top + insets.top + lp.topMargin,
right - insets.right - lp.rightMargin,
bottom - insets.bottom - lp.bottomMargin);
}
=========================================================================
回收是RecyclerView
的灵魂,也是RecyclerView
与普通ViewGroup
的区别。众所周知,RecyclerView中含有四类缓存,在布局过程中它们各自有各自的用途:
1、AttachedScrap:
存放可见、不需要重新绑定的ViewHolder
2、CachedViews
: 存放不可见、不需要重新绑定的ViewHoler
3、ViewCacheExtension
: 自定义缓存(存放不可见、不需要重新绑定)
4、RecyclerPool
: 存放不可见、需要重新绑定的ViewHolder
在LayoutManager
中提供了多个回收方法:
//将指定的View直接回收加至ecyclerPool
public void removeAndRecycleView(@NonNull View child, @NonNull Recycler recycler) {
removeView(child);
recycler.recycleView(child);
}
//将指定位置的View直接回收加至ecyclerPool
public void removeAndRecycleViewAt(int index, @NonNull Recycler recycler) {
final View view = getChildAt(index);
removeViewAt(index);
recycler.recycleView(view);
}
=================================================================================
1、实现抽抽象方法,并让RecyclerView
可横向滑动
public class RepeatLayoutManager extends RecyclerView.LayoutManager {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public boolean canScrollHorizontally() {
return true;
}
}
2、定义初始布局
在onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
方法中对ItemView
进行添加、测量、布局。
具体步骤如下:
-
使用
recycler.getViewForPosition(int pos)
从缓存中获取子View -
当可布局区域有多余的空间时,通过
addView(View view)
将对子View进行添加,通过在RecyclerView
中添加子View,并对子View进行测量与布局,直至子View超出RecyclerView
的可布局宽度。
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0) {
return;
}
if (state.isPreLayout()) {
return;
}
//将所有Item分离至scrap
detachAndScrapAttachedViews(recycler);
int itemLeft = getPaddingLeft();
for (int i = 0; ; i++) {
if (itemLeft >= getWidth() - getPaddingRight()) {
break;
}
View itemView = recycler.getViewForPosition(i % getItemCount());
//添加子View
addView(itemView);
//测量子View
measureChildWithMargins(itemView, 0, 0);
int right = itemLeft + getDecoratedMeasuredWidth(itemView);
int top = getPaddingTop();
int bottom = top + getDecoratedMeasuredHeight(itemView) - getPaddingBottom();
//对子View进行布局
layoutDecorated(itemView, itemLeft, top, right, bottom);
itemLeft = right;
}
}
3、滑动与填充
offsetChildrenHorizontal(int x)
用作对RecyclerView
中的子View进行整体左右移动。 为了在滑动RecyclerView
时有子View移动的效果,需要复写scrollHorizontallyBy
函数,并在其中调用offsetChildrenHorizontal(int x)
。
当左滑后子View被左移动时,RecyclerView
的右侧会出现可见的未填充区域,这时需要在RecyclerView
右侧添加并布局好新的子View,直到没有可见的未填充区域为止。
同样,在右滑后需要对左侧的未填充区域进行填充。
具体代码如下:
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
fill(recycler, dx > 0);
offsetChildrenHorizontal(-dx);
return dx;
}
/**
- 滑动的时候,填充可见的未填充区域
*/
private void fill(RecyclerView.Recycler recycler, boolean fillEnd) {
if (getChildCount() == 0) return;
if (fillEnd) {
//填充尾部
View anchorView = getChildAt(getChildCount() - 1);
int anchorPosition = getPosition(anchorView);
for (; anchorView.getRight() < getWidth() - getPaddingRight(); ) {
int position = (anchorPosition + 1) % getItemCount();
if (position < 0) position += getItemCount();
View scrapItem = recycler.getViewForPosition(position);
addView(scrapItem);
measureChildWithMargins(scrapItem, 0, 0);
int left = anchorView.getRight();
int top = getPaddingTop();
int right = left + getDecoratedMeasuredWidth(scrapItem);
int bottom = top + getDecoratedMeasuredHeight(scrapItem) - getPaddingBottom();
layoutDecorated(scrapItem, left, top, right, bottom);
anchorView = scrapItem;
}
} else {
//填充首部
View anchorView = getChildAt(0);
int anchorPosition = getPosition(anchorView);
for (; anchorView.getLeft() > getPaddingLeft(); ) {
int position = (anchorPosition - 1) % getItemCount();
if (position < 0) position += getItemCount();
View scrapItem = recycler.getViewForPosition(position);
addView(scrapItem, 0);
measureChildWithMargins(scrapItem, 0, 0);
int right = anchorView.getLeft();
int top = getPaddingTop();
int left = right - getDecoratedMeasuredWidth(scrapItem);
int bottom = top + getDecoratedMeasuredHeight(scrapItem) - getPaddingBottom();
layoutDecorated(scrapItem, left, top,
right, bottom);
anchorView = scrapItem;
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
img-Qs9IeJMq-1715797564562)]
[外链图片转存中…(img-AeWt5Tuy-1715797564563)]
[外链图片转存中…(img-fWX3daRK-1715797564564)]
[外链图片转存中…(img-0JUKJB5y-1715797564565)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!