Android RecyclerView 快速平滑返回顶部

先看下实现的效果,没效果什么都白扯


recyclerview滑动.gif

下面直接上方法:

//目标项是否在最后一个可见项之后
  private boolean mShouldScroll;
  //记录目标项位置
  private int mToPosition;
  //目标项是否在最后一个可见项之后 private boolean mShouldScroll; //记录目标项位置 private int mToPosition;
  //滑动到指定位置
  private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) { // 第一个可见位置
      int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
      // 最后一个可见位置
      int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));
      if (position < firstItem) {
          // 第一种可能:跳转位置在第一个可见位置之前
          mRecyclerView.smoothScrollToPosition(position);
      } else if (position <= lastItem) {
          // 第二种可能:跳转位置在第一个可见位置之后
          int movePosition = position - firstItem;
          if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
              int top = mRecyclerView.getChildAt(movePosition).getTop();
              mRecyclerView.smoothScrollBy(0, top);
          }
      } else {
          // 第三种可能:跳转位置在最后可见项之后
          mRecyclerView.smoothScrollToPosition(position);
          mToPosition = position;
          mShouldScroll = true;
      }
  }

使用方法

 smoothMoveToPosition(rcv_activity_qz_info_list, 0);

在使用方法的时候判断下非空等操作,避免无用操作

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 10,339评论 0 13
  • 孩子的中考分数下来了,不算差,也不是很好,大概每个作父母的都期望孩子会更好吧! 中考应该是她人生道路的第一...
    绿塬阅读 175评论 0 1
  • 大一的时候你跟我们说 你有男朋友了 他想和你结婚 他说他等你五年 从那以后我们都管他叫五年 现在你们真的要结婚了 ...
    花俏俏卌阅读 196评论 0 0

友情链接更多精彩内容