用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。
生活随笔
收集整理的這篇文章主要介紹了
用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
RecyclerView滾動到指定位置并置頂
RecyclerView本身提供了幾個定位的方法,除了手動滑動的scrollTo,smootScrollTo和scrollBy,smoothScrollBy方法之外,有一個直接滑動到指定位置item的scrollToPosition方法和另一個在此基礎(chǔ)上平滑滾動的smoothScrollToPosition方法。但是經(jīng)實驗,該方法只能保證指定位置的item滑動到屏幕可見,如果指定的item本來就已在屏幕可見范圍,則不會滑動,并且屏幕外的item滑到可見范圍后,還需手動置頂。
常見處理方式
看了網(wǎng)上大多數(shù)相關(guān)的博客,一般的處理都是將item區(qū)分為 在可見范圍以上/在可見范圍內(nèi)/在可見范圍以下 三種情況,分別進行處理。
1、item在第一個可見item之前,直接用smoothScrollToPosition,則當(dāng)該item移動到可見范圍時,它就在RecyclerView頂部
2、item在可見范圍內(nèi),即在第一個可見item之后,最后一個可見item之前,那么這時scrollToPosition失效,需要手動計算該item的view距離頂部的距離,用scrollBy自行移動到置頂位置
3、item在最后一個可見item之后,用smoothScrollToPosition滑動到可見范圍 (此時該item在最后一個位置),再獲取該item的view,計算到頂部距離,再監(jiān)聽RecyclerView的滑動,對其進行二次滑動到頂部
貼上該方法主要的實現(xiàn)代碼:
//標(biāo)記是否需要二次滑動private boolean shouldMove;//需要滑動到的item位置private int mPosition;/*** RecyclerView滑動到指定item函數(shù)*/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在可見范圍內(nèi),即在第一個可見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滑動到可見范圍// 再監(jiān)聽RecyclerView的滑動,對其進行二次滑動到頂部recyclerView.smoothScrollToPosition(position);mPositon = position;shouldMove = true;}}…………/*** 監(jiān)聽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);}}});本文推薦的另外一種處理方式
通過上面的代碼可以看出來,這種處理方式比較麻煩,而且處理邏輯需要分成兩塊,并不夠直觀。因此點開源碼,發(fā)現(xiàn)實際上RecyclerView在用smoothScrollToPosition函數(shù)時,是創(chuàng)建了一個LinearSmoothScroller:
再繼續(xù)點開看:
一進入文件就發(fā)現(xiàn)了SNAP_TO_START這個參數(shù),注釋意思是,將子view與父view左對齊或頂部對齊,其中是左對齊還是頂部對齊,是根據(jù)LayoutManager是horizontal還是vertical決定,因此重寫LinearSmoothScroller,設(shè)置該參數(shù)即可實現(xiàn)置頂。
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,調(diào)用startSmoothScroll即可
final TopSmoothScroller mTopScroller = new TopSmoothScroller(this); mTopScroller.setTargetPosition(position); mRecyclerView.getLayoutManager.startSmoothScroll(mTopScroller);總結(jié)
以上是生活随笔為你收集整理的用startSmoothScroll实现RecyclerView滚动到指定位置并置顶,含有动画。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想20015型号笔记本配置?
- 下一篇: 有源音箱和无源音箱哪个好?