【Android】自定义无限循环的LayoutManager(3)

child.layout(left + insets.left + lp.leftMargin, top + insets.top + lp.topMargin,

right - insets.right - lp.rightMargin,

bottom - insets.bottom - lp.bottomMargin);

}

recycle

=========================================================================

回收是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);

}

LayoutManager创建

=================================================================================

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;

}

}

return;

}

回收

====================================================================

前面讲到,当对RecyclerView进行滑动时,需要对可见的未填充区域进行填充。然而一直填充不做回收Item,那就和普通的ViewGroup没有太多的区别了。

RecyclerView中,需要在滑动、填充可见区域的同时,对不可见区域的子View进行回收,这样才能体现出RecyclerView的优势。

回收的方向与填充的方向恰好相反。那回收的代码具体如何实现呢?代码如下:

@Override

public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {

fill(recycler, dx > 0);

offsetChildrenHorizontal(-dx);

recyclerChildView(dx > 0, recycler);

return dx;

}

/**

  • 回收不可见的子View

*/

private void recyclerChildView(boolean fillEnd, RecyclerView.Recycler recycler) {

if (fillEnd) {

//回收头部

for (int i = 0; ; i++) {

View view = getChildAt(i);

boolean needRecycler = view != null && view.getRight() < getPaddingLeft();

if (needRecycler) {

removeAndRecycleView(view, recycler);

} else {

return;

}

}

} else {

//回收尾部

for (int i = getChildCount() - 1; ; i–) {

View view = getChildAt(i);

boolean needRecycler = view != null && view.getLeft() > getWidth() - getPaddingRight();

if (needRecycler) {

removeAndRecycleView(view, recycler);

} else {

return;

}

}

}

}

使用

====================================================================

最后

**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。



《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
cycler);

} else {

return;

}

}

}

}

使用

====================================================================

最后

**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。


[外链图片转存中…(img-YXTGQhPW-1715200766842)]

[外链图片转存中…(img-bOquIBXA-1715200766845)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值