昨天写了RecyclerView进阶之层叠列表(上),不过只实现了基本的效果。今天看到不少人点赞,因而我趁热打铁,把这个控件写完成吧。没看过前篇的同窗,先移步熟悉下吧。下篇的主要内容就是实现层叠列表边缘的层叠动画和RecyclerView的回收复用,也是这个控件实现的难点所在。java
关于这个,先上图吧:git
private @FloatRange(from = 0.01, to = 1.0)
float edgePercent = 0.5f;//触发边缘动画距离百分比
private @IntRange(from = 1)
int slowTimes = 5;//到达此距离后放慢倍数
复制代码
而后在滚动时从新布局列表各个ItemView位置的方法中处理动画,关键点是在ItemView到达边界临界点时,将本来的偏移值除以一个倍数,使其移动速度变慢:github
private void addAndLayoutViewVertical(RecyclerView.Recycler recycler, RecyclerView.State state, int offset) {
//反向遍历Recycler中保存的View取出来
for (int i = itemCount - 1; i >= 0; i--) {
Rect mTmpRect = allItemRects.get(i);
int bottomOffset = mTmpRect.bottom - offset;
int topOffset = mTmpRect.top - offset;
if (i != itemCount - 1) {//除最后一个外的底部慢速动画
if (displayHeight - bottomOffset <= height * edgePercent) {
//到达边界触发距离
int edgeDist = (int) (displayHeight - height * edgePercent);
//到达边界后速度放慢到原来5分之一,计算出实际须要的底部位置
int bottom = edgeDist + (bottomOffset - edgeDist) / slowTimes;
//固然这个位置不能超过底部
bottom=Math.min(bottom,displayHeight);
realBottomOffset = bottom;
}
} else {
// 若是是最后一个就不须要动画了,由于已经在底部了
realBottomOffset = totalHeight > displayHeight ? displayHeight : totalHeight;
}
}
}
复制代码
上面是底部边界动画的处理,顶部边界动画的处理也是同样的。如今咱们已经能够看到有层叠的效果了:bash
这个功能若是没有实现的话,就对不起RecyclerView的名号了。下面都只贴了核心代码:ide
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
//在每次触发滚动的时候,把全部View从RecyclerView移除掉,这样recyclerView就没有itemview了
detachAndScrapAttachedViews(recycler);
//重新布局位置、显示View
addAndLayoutViewVertical(recycler, state, verticalScrollOffset);
return tempDy;
}
复制代码
上面已经吧全部的itemView移除了,如今遍历全部的itemView,判断是否在屏幕中显示,是的话就从新添加进去,不是的话就跳过。由于有边缘层叠动画,咱们放慢了速度,可是比较的还是原来滚动的距离,overFlyingDist= slowTimes * height
;布局
private void addAndLayoutViewVertical(RecyclerView.Recycler recycler, RecyclerView.State state, int offset) {
int itemCount = getItemCount();
if (itemCount <= 0 || state.isPreLayout()) {
return;
}
int displayHeight = getVerticalSpace();
for (int i = itemCount - 1; i >= 0; i--) {
Rect mTmpRect = allItemRects.get(i);
// 遍历Recycler中保存的View取出来
int bottomOffset = mTmpRect.bottom - offset;
int topOffset = mTmpRect.top - offset;
boolean needAdd = true;//是否
if (bottomOffset - displayHeight >= overFlyingDist) {
//超过了底部最大距离
needAdd = false;
}
if (topOffset < -overFlyingDist && i != 0 && topOverFlying
|| topOffset < -overFlyingDist && !topOverFlying) {
//超过了顶部最大距离
needAdd = false;
}
if (needAdd) {
//开始布局
..........
}
Log.d(TAG, "childCount = " + getChildCount() + " itemCount= " + itemCount);
}
复制代码
打印日志看下结果,即便把列表总数加到1000项,recyclerView中的itemView数量也始终维持在实际显示数量附近,顺便也解决了由于Itemview没有回收,两边阴影层叠在一块儿造成黑边的问题:post
到这已经基本实现了咱们所指望的功能。可是和系统自带的三个layoutManager相比,还欠缺不少基本的对外操做方法,因此咱们来依照LinearLayoutManager
实现几个同名方法:动画
@Override
public View findViewByPosition(int position) {
final int childCount = getChildCount();
if (childCount == 0) {
return null;
}
final int firstChild = getPosition(getChildAt(0));
final int viewPosition = position - firstChild;
if (viewPosition >= 0 && viewPosition < childCount) {
final View child = getChildAt(viewPosition);
if (getPosition(child) == position) {
return child; // in pre-layout, this may not match
}
}
return super.findViewByPosition(position);
}
复制代码
requestLayout()
方法会调用onLayoutChildren()
,offsetUseful
用来标识onLayoutChildren()
时是否将参数重置ui
@Override
public void scrollToPosition(int position) {
Rect mTmpRect = allItemRects.get(position);
if (mTmpRect != null) {
offsetUseful = true;
if (orientation == OrientationHelper.VERTICAL) {
verticalScrollOffset = mTmpRect.top;
} else {
horizontalScrollOffset = mTmpRect.left;
}
}
requestLayout();
}
复制代码
好了至于别的方法,你们若是感兴趣的话本身去看源码吧!这个控件还有待完善的地方,我会持续补充,欢迎你们提Issues,或者给个star!this