iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果
  • 953
分享到

Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果

recyclerviewAndroid 2022-06-06 11:06:18 953人浏览 薄情痞子
摘要

本文中所有效果通过ItemDecoration实现,通过此实现的可以与业务解耦,让RecyclerView的模板更加简洁,不关心任何辅助性ui,GitHub地址 一、顶部吸附效

本文中所有效果通过ItemDecoration实现,通过此实现的可以与业务解耦,让RecyclerView的模板更加简洁,不关心任何辅助性ui,GitHub地址

一、顶部吸附效果图

所有都吸附

二、顶部不吸附效果图

顶部不吸附效果

三、不满一屏效果

不满一屏效果

四、核心实现点

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实现悬浮吸顶、分隔线、到底提示效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

您可能感兴趣的文章:RecyclerVIew实现悬浮吸顶效果android中RecyclerView悬浮吸顶效果如何为RecyclerView添加分隔线


--结束END--

本文标题: Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果

本文链接: https://www.lsjlt.com/news/28386.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作