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

本文详细介绍了如何创建一个自定义的无限循环RecyclerView LayoutManager,包括如何处理滑动、填充和回收视图,以及如何在滑动时保持性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、定义初始布局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值