不得不说RecyclerView真的很强大,例如无限轮播Banner,滑动卡片等都有RecyclerView的版本,他们是怎么做的呢?答案是基于RecyclerView.LayoutManager,我们可以自定义RecyclerView.LayoutManager,然后控制RecyclerView内部Item的位置以及大小达到我们想要的效果,为了简单,我们先自定义一个RecyclerView.LayoutManager,模仿LinearLayoutManager的效果。
开整:
- 1.自定义类TestLayoutManager继承RecyclerView.LayoutManager,实现抽象方法:
public class TestLayoutManager extends RecyclerView.LayoutManager {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return null;
}
}
- 2.generateDefaultLayoutParams()就是RecyclerView 子 item 的 LayoutParameters,一般包裹内容即可:
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
- 3.关键:重写onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)方法,对Item按照指定位置添加到视图中,有些类似于自定义ViewGroup的onLayout方法:
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//如果没有item,直接返回
if (getItemCount() <= 0) return;
// 跳过preLayout,preLayout主要用于支持动画
if (state.isPreLayout()) {
return;
}
//在布局之前,将所有的子View先Detach掉,放入到Scrap缓存中
detachAndScrapAttachedViews(recycler);
//定义竖直方向的偏移量
int offsetY = 0;
for (int i = 0; i < getItemCount(); i++) {
//这里就是从缓存里面取出
View view = recycler.getViewForPosition(i);
//将View加入到RecyclerView中
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
//最后,将View布局
int left = (getWidth() - width) / 2;
layoutDecorated(view, left, offsetY, getWidth() - left, offsetY + height);
//将竖直方向偏移量增大height
offsetY += height;
}
}
运行效果:
- 4.此时,Item能正常显示,但是无法响应滚动事件,所以还需要处理滚动事件,重写canScrollVertically()以及scrollVerticallyBy()方法:
@Override
public boolean canScrollVertically() {
return true;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
offsetChildrenVertical(dy);
return dy;
}
- 5.此时Item可以滚动了,但是有两个问题,a.滑动方向与手势相反 b.滑动没有边界限制 编辑下面代码解决问题 :
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
//实际要滑动的距离
int travel = dy;
//如果滑动到最顶部
if (verticalScrollOffset + dy < 0) {
travel = -verticalScrollOffset;
} else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {//如果滑动到最底部
travel = totalHeight - getVerticalSpace() - verticalScrollOffset;
}
//将竖直方向的偏移量+travel
verticalScrollOffset += travel;
// 平移容器内的item
offsetChildrenVertical(-travel);
return travel;
}
/**
* 获取RecyclerView在垂直方向上的可用空间,即去除了padding后的高度
*
* @return
*/
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
现在除了还没做Item缓存,基本和LinearLayoutManager效果差不多了。
完整源码:
/**
* Created by Chao 2018/8/15 on 16:15
* description
*/
public class TestLayoutManager extends RecyclerView.LayoutManager {
private int totalHeight = 0;
private int verticalScrollOffset = 0;
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//如果没有item,直接返回
if (getItemCount() <= 0) return;
// 跳过preLayout,preLayout主要用于支持动画
if (state.isPreLayout()) {
return;
}
//在布局之前,将所有的子View先Detach掉,放入到Scrap缓存中
detachAndScrapAttachedViews(recycler);
//定义竖直方向的偏移量
int offsetY = 0;
totalHeight = 0;
for (int i = 0; i < getItemCount(); i++) {
//这里就是从缓存里面取出
View view = recycler.getViewForPosition(i);
//将View加入到RecyclerView中
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
//最后,将View布局
int left = (getWidth() - width) / 2;
layoutDecorated(view, left, offsetY, getWidth() - left, offsetY + height);
//将竖直方向偏移量增大height
offsetY += height;
totalHeight += height;
}
}
@Override
public boolean canScrollVertically() {
return true;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
//实际要滑动的距离
int travel = dy;
//如果滑动到最顶部
if (verticalScrollOffset + dy < 0) {
travel = -verticalScrollOffset;
} else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {//如果滑动到最底部
travel = totalHeight - getVerticalSpace() - verticalScrollOffset;
}
//将竖直方向的偏移量+travel
verticalScrollOffset += travel;
// 平移容器内的item
offsetChildrenVertical(-travel);
return travel;
}
/**
* 获取RecyclerView在垂直方向上的可用空间,即去除了padding后的高度
*
* @return
*/
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}