一般我们用 mRecycleview.smoothScrollToPosition(0)滑动到顶部,具有滚动效果,但是如果我们想滚动到任意指定位置,那么smoothScrollToPosition()就不能保证所指定item位于屏幕顶部,那么一下提供下我解决的方法:

1.第一种方法

此方法能实现指定位置位于屏幕顶部,但是不具有平滑滚动视觉效果:

 if (position != -1) {
mRecycleview.scrollToPosition(position);
LinearLayoutManager mLayoutManager =
(LinearLayoutManager) mRecycleview.getLayoutManager();这里的LinearLayoutManager对象只能是动态获取,不能用全局的。
 mLayoutManager.scrollToPositionWithOffset(position, 0); }

2.第二种方法

此方法能实现指定位置位于屏幕顶部,具有平滑滚动视觉效果:

首先获取第一个可见位置和最后一个可见位置,分三种情况:

1.如果如果跳转位置在第一个可见位置之前,就smoothScrollToPosition()可以直接跳转; 
2.如果跳转位置在第一个可见项之后,最后一个可见项之前smoothScrollToPosition()不会滚动,此时调用smoothScrollBy来滑动到指定位置; 
3.如果要跳转的位置在最后可见项之后,则先调用smoothScrollToPosition()将要跳转的位置滚动到可见位置,在addOnScrollListener()里通过onScrollStateChanged控制,调用smoothMoveToPosition,再次执行判断;

 //目标项是否在最后一个可见项之后
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;
}
}
 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (mShouldScroll) {
mShouldScroll = false;
smoothMoveToPosition(irc, mToPosition);
}
}
});
if (position != -1) {
smoothMoveToPosition(irc,position);
}else {
smoothMoveToPosition(irc,position+1);
} 改文章出自:https://blog.csdn.net/shanshan_1117/article/details/78780137
特此感谢!

RecyclerView滑动到指定位置,并置顶的更多相关文章

  1. JavaScript学习笔记-元素在滚动条滑动一定高度后自动置顶

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. jquery实现点击按钮滑动到指定位置

    <body> <script type="text/javascript"> function click_scroll() { var scroll_of ...

  3. 鼠标滑动到指定位置时div固定在头部

    $(function(){ $(window).scroll(function () {            if ($(window).scrollTop() > 253) {       ...

  4. JQuery滑动到指定位置

    $('html, body').animate({ scrollTop: next_tip.offset().top + "px"},500);

  5. RecyclerView跳转到指定位置的三种方式

    自从android5.0推出RecyclerView以后,RecyclerView越来越受广大程序员的热爱了!大家都知道RecyclerView的出现目的是为了替代listview和ScrollVie ...

  6. 横向滑动的HorizontalListView滑动指定位置的解决方法

    项目中用到了自定义横向滑动的控件:HorizontalListView,点击其中一项,跳转到另外一个大图界面,大图界面也是HorizontalListView,想使用setSelection方法设定 ...

  7. jQuery实现将div中滚动条滚动到指定位置的方法

    1.JS代码: onload = function () { //初始化 scrollToLocation(); }; function scrollToLocation() { var mainCo ...

  8. RecyclerView实现侧滑删除、置顶、滑动

    1.首先在build.gradle里添加 compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1' 2.设置recyclerView的item布 ...

  9. 在UWP中页面滑动导航栏置顶

    最近在研究掌上英雄联盟,主要是用来给自己看新闻,顺便copy个界面改一下段位装装逼,可是在我copy的时候发现这个东西 当你滑动到一定距离的时候导航栏会置顶不动,这个特性在微博和淘宝都有,我看了@ms ...

随机推荐

  1. shell 基础(一)

    废话少说 往下看 1. 查看 Shell Shell 是一个程序,一般都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中./e ...

  2. linux18.04下安装的jdk11.0.2

    1.百度搜索jdk,选择jdk11.0.2,操作如下图: 2.下载完成,ctrl+alt+t打开终端并在/usr/local创建java文件夹 cd /usr/local sudo mkdir /us ...

  3. 关于confluence上传文件附件预览查看时出现乱码的问题解决办法

    在confluence上传excel文件,预览时发现乱码问题主要是因为再上传文件的时候一般是Windows下的文件上传,而预览的时候,是linux下的环境,由于linux下没有微软字体,所以预览的时候 ...

  4. python time模块和datetime模块

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  5. HDU 6108(整除判断 数学)

    题意是求在给定的 P 进制下,满足条件的数字 B 有多少.条件:若任何一个数的各位数字之和能被 B 整除,则该数可被 B 整除. 在 p 进制下,每个正整数都可以都可以表示为:a0*p^0 + a1* ...

  6. Python 各种进制转换

    #coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...

  7. SpringBoot系列: CommandLineRunner接口的用处

    ========================================使用 CommandLineRunner 对Spring Bean进行额外初始化==================== ...

  8. sqlmap基础入门超详细教程

    前言: 总算进入了自己喜欢的行业. 要时刻记得当初自己说过的话, 不忘初心. Come on! 资料: 感谢超哥分享的干货..  sqlmap干货点击直达 学习环境: 本次学习使用的是kali集成的s ...

  9. requests.session

    # -*- coding: utf-8 -*- """requests.session~~~~~~~~~~~~~~~~ This module provides a Se ...

  10. java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or.....

    1,<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dia ...