private List groupList = new ArrayList<>();//用户设置的分组列表
private Map<Object,GroupItem> groups = new HashMap<>();//保存startPosition与分组对象的对应关系
private int[] groupPositions;//保存分组startPosition的数组
private int positionIndex;//分组对应的startPosition在groupPositions中的索引
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if(!isLinearAndVertical(parent)){//若RecyclerView类型不是LinearLayoutManager.VERTICAL,跳出(下同)
return;
}
if(isFirst){
measureView(groupView,parent);//绘制View需要先测量View的大小及相应的位置
decorationCallback.setGroup(groupList);//获取用户设置的分组列表
if(groupList.size()==0){//若用户没有设置分组,跳出(下同)
return;
}
groupPositions = new int[groupList.size()];
positionIndex = 0;
int a = 0;
for(int i=0;i<groupList.size();i++){//保存groupItem与其startPosition的对应关系
int p = groupList.get(i).getStartPosition();
if(groups.get§==null){
groups.put(p,groupList.get(i));
groupPositions[a] = p;
a++;
}
}
isFirst = false;
}
int position = parent.getChildAdapterPosition(view);
if(groups.get(position)!=null){
//若RecyclerView中该position对应的childView之前需要绘制groupView,则为其预留相应的高度空间
outRect.top = groupViewHeight;
}
}
/**
- 测量View的大小和位置
- @param view
- @param parent
*/
private void measureView(View view,View parent){
if (view.getLayoutParams() == null) {
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
parent.getPaddingLeft() + parent.getPaddingRight(), view.getLayoutParams().width);
int childHeight;
if(view.getLayoutParams().height > 0){
childHeight = View.MeasureSpec.makeMeasureSpec(view.getLayoutParams().height, View.MeasureSpec.EXACTLY);
} else {
childHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);//未指定
}
view.measure(childWidth, childHeight);
view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
groupViewHeight = view.getMeasuredHeight();
}
/**
- 判断LayoutManager类型,目前GroupItemDecoration仅支持LinearLayoutManager.VERTICAL
- @param parent
- @return
*/
private boolean isLinearAndVertical(RecyclerView parent){
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (!(layoutManager instanceof LinearLayoutManager)) {
return false;
}else {
if(((LinearLayoutManager) layoutManager).getOrientation()
!= LinearLayoutManager.VERTICAL){
return false;
}
}
return true;
}
}
在RecyclerView为GroupView预留了空间后,我们需要重写onDraw方法将其绘制出来。为了保证将所有用户设置的分组都绘制出来,我们要遍历RecyclerView所有的childView,当循环到该childView的position能找到对应的GroupItem时,便在该childView的上方绘制出GroupView(该位置正是之前预留的空间),具体代码如下
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if(groupList.size()==0 || !isLinearAndVertical(parent)){
return;
}
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
float left = child.getLeft();
float top = child.getTop();
int position = parent.getChildAdapterPosition(child);
if(groups.get(position)!=null){
c.save();
c.translate(left,top - groupViewHeight);//将画布起点移动到之前预留空间的左上角
decorationCallback.buildGroupView(groupView,groups.get(position));//通过接口回调得知GroupView内部控件的数据
measureView(groupView,parent);//因为内部控件设置了数据,所以需要重新测量View
groupView.draw©;
c.restore();
}
}
}
接下来是绘制粘性头部,由两部分特效构成
- 保持当前childView对应的分组GroupView始终保持在RecyclerView顶部
- 当用户滑动RecyclerView使得上一组或下一组的GroupView“碰撞”到顶部的GroupView时,将会朝用户滑动的方向将其推开
推动特效主要是通过相邻组GroupView的top位置关系来实现,为了更好地理解相邻组的关系及接下来的代码逻辑,博主简单介绍一下分组逻辑:
RecyclerView可视范围(当前屏幕中显示的)内的分组划分为“上一组(pre)”、“当前组(cur)”和“下一组(next)”,这三组的划分依据如下
- next组由cur组决定,跟在cur组后的那一组就是next组
- RecyclerView最上方的childView如果是某组的第一个child,则该组为cur组,若该childView完全离开屏幕,则该组为pre组,按顺序其后面的组就为cur组
具体代码如下(表达能力有限,实在没搞明白的童鞋可以调试一下代码看看各个判断分支的跳入时机):
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if(groupList.size()==0 || !isStickyHeader || !isLinearAndVertical(parent)){
return;
}
int childCount = parent.getChildCount();
Map<Object,Object> map = new HashMap<>();
//遍历当前可见的childView,找到当前组和下一组并保存其position索引和GroupView的top位置
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
float top = child.getTop();
int position = parent.getChildAdapterPosition(child);
if(groups.get(position)!=null){
positionIndex = searchGroupIndex(groupPositions,position);
if(map.get(“cur”)==null){
map.put(“cur”, positionIndex);
map.put("
curTop",top);
}else {
if(map.get(“next”)==null){
map.put(“next”, positionIndex);
map.put(“nextTop”,top);
}
}
}
}
c.save();
if(map.get(“cur”)!=null){//如果当前组不为空,说明RecyclerView可见部分至少有一个GroupView
indexCache = (int)map.get(“cur”);
float curTop = (float)map.get(“curTop”);
if(curTop-groupViewHeight<=0){//保持当前组GroupView一直在顶部
curTop = 0;
}else {
map.put(“pre”,(int)map.get(“cur”)-1);
if(curTop - groupViewHeight < groupViewHeight){//判断与上一组的碰撞,推动当前的顶部GroupView
curTop = curTop - groupViewHeight*2;
}else {
curTop = 0;
}
indexCache = (int)map.get(“pre”);
}
if(map.get(“next”)!=null){
float nextTop = (float)map.get(“nextTop”);
if(nextTop - groupViewHeight < groupViewHeight){//判断与下一组的碰撞,推动当前的顶部GroupView
curTop = nextTop - groupViewHeight*2;
}
}
c.translate(0,curTop);
if(map.get(“pre”)!=null){//判断顶部childView的分组归属,绘制对应的GroupView
drawGroupView(c,parent,(int)map.get(“pre”));
}else {
drawGroupView(c,parent,(int)map.get(“cur”));
}
}else {//否则当前组为空时,通过之前缓存的索引找到上一个GroupView并绘制到顶部
c.translate(0,0);
drawGroupView(c,parent,indexCache);
}
c.restore();
}
/**
- 绘制GroupView
- @param canvas
- @param parent
- @param index
*/
private void drawGroupView(Canvas canvas,RecyclerView parent,int index){
if(index<0){
return;
}
decorationCallback.buildGroupView(groupView,groups.get(groupPositions[index]));
measureView(groupView,parent);
groupView.draw(canvas);
}
/**
- 查询startPosition对应分组的索引
- @param groupArrays
- @param startPosition
- @return
*/
private int searchGroupIndex(int[] groupArrays, int startPosition){
sureView(groupView,parent);
groupView.draw(canvas);
}
/**
- 查询startPosition对应分组的索引
- @param groupArrays
- @param startPosition
- @return
*/
private int searchGroupIndex(int[] groupArrays, int startPosition){