Android RecyclerView 快速平滑返回顶部

摘要:
先看下实现的效果,没效果什么都白扯下面直接上方法://目标项是否在最后一个可见项之后privatebooleanmShouldScroll;//记录目标项位置privateintmToPosition;//目标项是否在最后一个可见项之后privatebooleanmShouldScroll;//记录目标项位置privateintmToPosition;//滑动到指定位置privatevoidsmo

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

Android RecyclerView 快速平滑返回顶部第1张

下面直接上方法:

//目标项是否在最后一个可见项之后
  private booleanmShouldScroll;
  //记录目标项位置
  private intmToPosition;
  //目标项是否在最后一个可见项之后 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);

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

免责声明:文章转载自《Android RecyclerView 快速平滑返回顶部》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇laravel文件上传Unix,windows和Mac中的换行下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CSS基础学习

盒模型 CSS 框模型 (Box Model) 规定了元素框处理元素内容(element)、内边距(padding)、边框(border) 和 外边距(margin) 的方式。 注意:外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距。 合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者。 定位 position 属性 通过使用po...

安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用

转载:  CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个ViewGroup容器组件,是为了Material Design而设计的App Bar,支持手势滑动操作。 默认的AppBarLayout是垂直方向的,它的作用...

Unity 3D 的四种坐标系

1, World Space(世界坐标): 我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。transform.position可以获得该位置坐标。 2, Screen Space(屏幕坐标): 以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位...

使用swiper.js实现移动端tab切换

在项目中遇到的,要实现tab切换,我用的是swiper.js 官网:http://www.swiper.com.cn/api/start/new.html 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-...

ggplot2绘图入门系列之三:位置调整与条形图

    位置调整(Position adjustments)是针对同一图层内元素的位置进行微调的方法。它包括五种设置,分别是stack、dodge、fill、identity、jitter。 我们用条形图来展示其用法,仍使用mpg数据集,其中用到的变量是class,即生产汽车的类型,以及year生产年份。下面的条形图是将各类型的汽车数量进行汇集,并以年份作...

Unity3D 学习教程 12 C# 发射炮弹

建立一个炮弹 一个球体 双击脚本 进入编辑器 1 using UnityEngine; 2 using System.Collections; 3 4 public class acc : MonoBehaviour { 5 6 // Use this for initialization 7 public Trans...