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