RecycleView滚动定位不准确问题研究

  • 开发当中经常会遇到的产品需求,recycleview自动滚动到某一个位置。具体场景可能是如下:
  • 页面初始化之后自动自动跳转到某一个位置
  • 页面滚动之后要回到某一个位置
  • 为了展示完全需要recycleview做微小的位置偏移等

针对如上问题,我们先来看看recycleview都有什么方法提供给我们。

1public void scrollToPosition(int position) { 2 if (mLayoutSuppressed) { 3 return; 4 } 5 stopScroll(); 6 if (mLayout == null) { 7 Log.e(TAG, "Cannot scroll to position a LayoutManager set. " 8 + "Call setLayoutManager with a non-null argument."); 9 return; 10 } 11 mLayout.scrollToPosition(position); 12 awakenScrollBars(); 13 } 14

scrollToPosition(int position)方法,滑动到指定positionitem的顶部。

1public void smoothScrollToPosition(int position) { 2 if (mLayoutSuppressed) { 3 return; 4 } 5 if (mLayout == null) { 6 Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " 7 + "Call setLayoutManager with a non-null argument."); 8 return; 9 } 10 mLayout.smoothScrollToPosition(this, mState, position); 11 } 12

smoothScrollToPosition(int position) 方法同上,但是会有平滑滑动的效果。

1public void scrollBy(int x, int y) { 2 if (mLayout == null) { 3 Log.e(TAG, "Cannot scroll without a LayoutManager set. " 4 + "Call setLayoutManager with a non-null argument."); 5 return; 6 } 7 if (mLayoutSuppressed) { 8 return; 9 } 10 final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); 11 final boolean canScrollVertical = mLayout.canScrollVertically(); 12 if (canScrollHorizontal || canScrollVertical) { 13 scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0, null); 14 } 15 } 16

scrollBy(int x, int y)方法通过传入偏移量进行滑动。

1public void startSmoothScroll(SmoothScroller smoothScroller) { 2 if (mSmoothScroller != null && smoothScroller != mSmoothScroller 3 && mSmoothScroller.isRunning()) { 4 mSmoothScroller.stop(); 5 } 6 mSmoothScroller = smoothScroller; 7 mSmoothScroller.start(mRecyclerView, this); 8 } 9

startSmoothScroll(SmoothScroller smoothScroller)通过传入一个SmoothScroller来控制recycleview的移动。

针对题前所说的三种情况来说 这个需求会涉及到几种情况如下:

  • 目标item已经出现在屏幕当中的情况,这时候调用方法一,方法二是达不到recycleview进行位置的移动的。这时候可以选择调用方法三来达到我们要的效果。
1 val smoothScroller: RecyclerView.SmoothScroller = object : LinearSmoothScroller(mContext) { 2 override fun getVerticalSnapPreference(): Int { 3 return SNAP_TO_START 4 } 5 } 6 7 smoothScroller.targetPosition = CIRCLE_POSITION + 1 8 mVirtualLayoutManager.startSmoothScroll(smoothScroller) 9

当然方法四也可以达到我们所要的效果,这时候需要算出来的是我们目标item距离顶部所需的距离

1rv.addOnScrollListener(new RecyclerView.OnScrollListener() { 2 @Override 3 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 4 super.onScrolled(recyclerView, dx, dy); 5 ...... 6 int toTop = rv.getChildAt(n).getTop(); 7 rvProduct.scrollBy(0, top); 8 ...... 9 } 10}); 11
  • 当目标item如果说并没有出现在屏幕中时,方法一和方法二就可以达到我们需要的效果。
点赞
收藏

评论区

加载中...

相关推荐

前端 滚动到目标元素位置 VUE 版本

1、前言想起之前一次在写一个小程序商城时候,详情页的类似锚点的跳转花了我不少时间,因为刚写,对小程序滚动,滚动距离那套不熟悉加之本身对什么滚动高度,元素距离顶部距离不是很了解,花了挺长时间的,这几天有空,就研究了下。2、先看效果在这里插入图片描述(https://imghelloworld.osscnbeijing.aliyuncs.c

cookie 时效无限延长方案

自动化测试有2种形式,接口自动化和UI自动化。而UI自动化经常会被登录节点堵塞,例如验证码、图形、滑块等,尽管有些方式可以识别图形和定位滑块位置,但成功率都不高,无法真正意义上实现自动化执行;而http接口的自动化测试前置如果依赖cookie,也无法实现自动化执行。

Python format 用法详解

一、填充字符串1\.位置print("hello{0},thisis{1}.".format("world","python"))根据位置下标进行填充print("hello{},thisis{}.".format("world","python"))根据顺序自动填充

unity 使物体跟随路径点自动移动位置

可以做全自动的动画,只要设置好路径点就可以了,privateTransformways1;//路径点的位置publicTransformtagret;//移动的物体privateintindex1;privatevoidStart(){

Axure中怎么制作锚点

实现目标点击导航条不同的菜单,页面跳到菜单相应的位置,实现页面自动滚动。1. 拖入编辑区3个矩形,并相应命名为菜单1,菜单2,菜单3;!(https://oscimg.oschina.net/oscnet/06f0b4fbaf28e43de8bbf44d62d356f0f27.png)2. 再新建3个想要制作的文本框准备进行

DragonBonesPro小游戏

1.开场动画首先将素材导入到DragonBones中,然后将各个素材图片移动到合适的位置,然后调整图层上下位置,并在20帧左右插入关键帧然后在0帧放入各素材进入背景时的位置以及时间顺序,最后就是创建补件动画调整各素材的大小变化!(https://oscimg.oschina.net/oscnet/upf664d9444d9254f2