日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。

發布時間:2023/12/3 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

RecyclerView滾動到指定位置并置頂

RecyclerView本身提供了幾個定位的方法,除了手動滑動的scrollTo,smootScrollTo和scrollBy,smoothScrollBy方法之外,有一個直接滑動到指定位置item的scrollToPosition方法和另一個在此基礎上平滑滾動的smoothScrollToPosition方法。但是經實驗,該方法只能保證指定位置的item滑動到屏幕可見,如果指定的item本來就已在屏幕可見范圍,則不會滑動,并且屏幕外的item滑到可見范圍后,還需手動置頂。
常見處理方式
看了網上大多數相關的博客,一般的處理都是將item區分為 在可見范圍以上/在可見范圍內/在可見范圍以下 三種情況,分別進行處理。
1、item在第一個可見item之前,直接用smoothScrollToPosition,則當該item移動到可見范圍時,它就在RecyclerView頂部
2、item在可見范圍內,即在第一個可見item之后,最后一個可見item之前,那么這時scrollToPosition失效,需要手動計算該item的view距離頂部的距離,用scrollBy自行移動到置頂位置
3、item在最后一個可見item之后,用smoothScrollToPosition滑動到可見范圍 (此時該item在最后一個位置),再獲取該item的view,計算到頂部距離,再監聽RecyclerView的滑動,對其進行二次滑動到頂部
貼上該方法主要的實現代碼:
//標記是否需要二次滑動private boolean shouldMove;//需要滑動到的item位置private int mPosition;/*** RecyclerView滑動到指定item函數*/private void smoothMoveToPosition(RecyclerView recyclerView, final int position) {// 獲取RecyclerView的第一個可見位置int firstItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));// 獲取RecyclerView的最后一個可見位置int lastItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(mRecyclerView.getChildCount() - 1));if (position < firstItem) {// 指定item在第一個可見item之前recyclerView.smoothScrollToPosition(position);} else if (position <= lastItem) {// 指定item在可見范圍內,即在第一個可見item之后,最后一個可見item之前int position = position - firstItem;if (position >= 0 && position < recyclerView.getChildCount()) {// 計算指定item的view到頂部的距離int top = recyclerView.getChildAt(position).getTop();// 手動滑動到頂部recyclerView.smoothScrollBy(0, top);}} else {// 指定item在最后一個可見item之后,用smoothScrollToPosition滑動到可見范圍// 再監聽RecyclerView的滑動,對其進行二次滑動到頂部recyclerView.smoothScrollToPosition(position);mPositon = position;shouldMove = true;}}…………/*** 監聽RecyclerView的滑動,對需要進行二次滑動的item進行滑動**/mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {@Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) {super.onScrollStateChanged(recyclerView, newState);if ( shouldMove && RecyclerView.SCROLL_STATE_IDLE == newState) {shouldMove = false;smoothMoveToPosition(mRecyclerView, mPosition);}}});
本文推薦的另外一種處理方式
通過上面的代碼可以看出來,這種處理方式比較麻煩,而且處理邏輯需要分成兩塊,并不夠直觀。因此點開源碼,發現實際上RecyclerView在用smoothScrollToPosition函數時,是創建了一個LinearSmoothScroller:

再繼續點開看:

一進入文件就發現了SNAP_TO_START這個參數,注釋意思是,將子view與父view左對齊或頂部對齊,其中是左對齊還是頂部對齊,是根據LayoutManager是horizontal還是vertical決定,因此重寫LinearSmoothScroller,設置該參數即可實現置頂。
public class TopSmoothScroller extends LinearSmoothScroller {TopSmoothScroller(Context context) {super(context);}@Overrideprotected int getHorizontalSnapPreference() {return SNAP_TO_START;}@Overrideprotected int getVerticalSnapPreference() {return SNAP_TO_START; // 將子view與父view頂部對齊} }
之后獲取RecyclerView的LayoutManager,調用startSmoothScroll即可
final TopSmoothScroller mTopScroller = new TopSmoothScroller(this); mTopScroller.setTargetPosition(position); mRecyclerView.getLayoutManager.startSmoothScroll(mTopScroller);

總結

以上是生活随笔為你收集整理的用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。