zoukankan      html  css  js  c++  java
  • Android RecyclerView 快速平滑返回顶部

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

    下面直接上方法:

    //目标项是否在最后一个可见项之后
      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);

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

  • 相关阅读:
    AW245 你能回答这些问题吗(连续子段和线段树)
    AW256 最大异或和(可持久化0/1trie树)
    AW247 亚特兰蒂斯(区间覆盖线段树)
    P1616 疯狂的采药
    P1060 开心的金明
    AW252 树(点分治)
    AW250 磁力块(分块)
    php绘图(一)
    判断一个文件里面有多少各种格式的图片
    添加图片水印图标
  • 原文地址:https://www.cnblogs.com/dingxiansen/p/10605664.html
Copyright © 2011-2022 走看看