Android中如何使用RecyclerView实现悬浮吸顶、分隔线、到底提示效果

小编给大家分享一下Android中如何使用RecyclerView实现悬浮吸顶、分隔线、到底提示效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!成都创新互联具备承接各种类型的做网站、成都网站建设项目的能力。经过十载的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

核心实现点

1、为什么通过ItemDecoration能够实现,原理?

①通过getItemOffsets()方法获取当前模板view的left、top、right、bottom边距,这些留出的间距用于绘制这些辅助性ui。

// RecyclerView的measure child方法public void measureChild(@NonNull View child, int widthUsed, int heightUsed) {      final LayoutParams lp = (LayoutParams) child.getLayoutParams();  //将getItemOffsets()获取的值累加到测量值之中      final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);      widthUsed += insets.left + insets.right;      heightUsed += insets.top + insets.bottom;      final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(),          getPaddingLeft() + getPaddingRight() + widthUsed, lp.width,          canScrollHorizontally());      final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),          getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,          canScrollVertically());      if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) {        child.measure(widthSpec, heightSpec);      }    }

②通过onDrawOver()绘制悬浮视图,绘制的ui在所有子视图之上。

@Override  public void draw(Canvas c) {    super.draw(c); //在RecyclerView绘制完之后回调onDrawOver()方法    final int count = mItemDecorations.size();    for (int i = 0; i < count; i++) {      mItemDecorations.get(i).onDrawOver(c, this, mState);    } }

③通过onDraw()方法绘制分割线等视图。

public void onDraw(Canvas c) {    super.onDraw(c); //先回调onDraw()方法,在绘制RecyclerView子view    final int count = mItemDecorations.size();    for (int i = 0; i < count; i++) {      mItemDecorations.get(i).onDraw(c, this, mState);    }  }

2、“到底提示” 的绘制

由于在getItemOffsets()获取不到子视图的宽高,此时还没有measure,在getItemOffsets()添加高度后,如果不满一屏需要在onDraw()方法中进行修正,修正方式为:反射修改mDecorInsets属性,重置在getItemOffsets()方法中设置的值。

private void setDecorInsetsBottom(RecyclerView.LayoutParams param, int bottom) {    try {      // 找到RecyclerView.LayoutParams中的mDecorInsets属性值      Field filed = RecyclerView.LayoutParams.class.getDeclaredField("mDecorInsets");      filed.setAccessible(true);      Rect decorRect = (Rect) filed.get(param);      decorRect.bottom = bottom;    } catch (Exception e) {    }  }

以上是“Android中如何使用RecyclerView实现悬浮吸顶、分隔线、到底提示效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

分享题目:Android中如何使用RecyclerView实现悬浮吸顶、分隔线、到底提示效果
网页地址:https://www.cdcxhl.com/article42/pdidhc.html

成都网站建设公司_创新互联,为您提供网站建设网站策划外贸建站定制网站企业网站制作用户体验

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设