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;
}
}
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;
}
}
}
}
====================================================================
- 添加依赖
implementation ‘cn.student0.manager:repeatmanager:1.0.2’
- 在代码中使用
RecyclerView recyclerView = findViewById(R.id.rv_demo);
recyclerView.setAdapter(new DemoAdapter());
recyclerView.setLayoutManager(new RepeatLayoutManager
====================================================================
到此,无限循环的LayoutManager
的实现已经完成。文章的不足还请指出,谢谢大家。
大家如果还想了解更多Android 相关的更多知识点,可以点进我的【GitHub项目中:https://github.com/733gh/GH-Android-Review-master】自行查看,里面记录了许多的Android 知识点。
结尾
最后,针对上面谈的内容,给大家推荐一个Android资料,应该对大家有用。
首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)
泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶
注解原理丶注解原理丶序列化
Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)
代码框架结构优化(数据结构丶排序算法丶设计模式)
APP性能优化(用户体验优化丶适配丶代码调优)
热修复丶热升级丶Hook技术丶IOC架构设计
NDK(c编程丶C++丶JNI丶LINUX)
如何提高开发效率?
MVC丶MVP丶MVVM
微信小程序
Hybrid
Flutter
接下来是资料清单:(敲黑板!!!)
1.数据结构和算法
2.设计模式
3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记
4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)
不论遇到什么困难,都不应该成为我们放弃的理由!共勉~
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
-1714974597476)]
2.设计模式
[外链图片转存中…(img-Ae0DtVUu-1714974597477)]
3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记
[外链图片转存中…(img-Q5HVljgS-1714974597478)]
4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)
[外链图片转存中…(img-u9zWvRa7-1714974597483)]
不论遇到什么困难,都不应该成为我们放弃的理由!共勉~
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-Avmok51G-1714974597486)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!