栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 从零开始实现RecyclerView分组及粘性头部效果,《Android面试题及解析》分享给大家

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 从零开始实现RecyclerView分组及粘性头部效果,《Android面试题及解析》分享给大家

groupList.add(groupItem);
}

@Override
public void buildGroupView(View groupView, GroupItem groupItem) {
//构建groupView,通过groupView.findViewById找到内部控件(暂不支持点击事件等),例如
TextView textName = (TextView) groupView.findViewById(R.id.text_name);
textName.setText(groupItem.getData(“name”).toString());
}
}));

如果还是不清楚可以去看下demo


实现思路

在我们自定义ItemDecoration之前首先得了解ItemDecoration有什么用,不清楚的可以看下这两篇博客

RecyclerView之ItemDecoration由浅入深

深入理解 RecyclerView 系列之一:ItemDecoration

简单来说,我们实现分组及粘性头部效果分三步

  1. 重写ItemDecoration.getItemOffsets在RecyclerView中为GroupView预留位置
  2. 重写ItemDecoration.onDraw在上一步预留的位置中绘制GroupView
  3. 重写ItemDecoration.onDrawOver绘制顶部悬停的GroupView(粘性头部)

我们按顺序一步步讲,首先,创建GroupItemDecoration继承自ItemDecoration,在初始化方法中获取用户设置的GroupView,并提供接口给用户设置分组相关

public class GroupItemDecoration extends RecyclerView.ItemDecoration {
private Context context;
private View groupView;
private DecorationCallback decorationCallback;

public GroupItemDecoration(Context context,View groupView,DecorationCallback decorationCallback) {
this.context = context;
this.groupView = groupView;
this.decorationCallback = decorationCallback;
}

public interface DecorationCallback {

void setGroup(List groupList);


void buildGroupView(View groupView, GroupItem groupItem);
}
}

然后重写getItemOffsets方法,根据用户设置的分组为GroupView预留位置,其中最主要的是测量出GroupView的宽高和位置。measureView方法中按着View的绘制顺序调用View.measure和View.layout,只有先完成了这两步,才能将View绘制到屏幕上,关于如何测量View大家可以看下这篇博客Android如何在初始化的时候获取加载的布局的宽高。接下来是具体的实现代码

public class GroupItemDecoration extends RecyclerView.ItemDecoration {
//省略部分代码…
private List groupList = new ArrayList<>();//用户设置的分组列表
private Map 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 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;
}
}


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();
}


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 map = new HashMap<>();

//遍历当前可见的childView,找到当前组和下一组并保存其position索引和GroupView的top位置
for

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

(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 {

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号