自從android5.0推出RecyclerView以后,RecyclerView越來越受廣大程序員的熱愛了!大家都知道RecyclerView的出現目的是為了替代listview和ScrollView在列表方面的使用!那么listview和ScrollView的所有功能和方法都應該有的!
但是RecyclerView的很多方法,不是封裝在RecyclerView中的,當我們在RecyclerView中找不到對應的方法時,就應該想到他的管理類manager了!
大多方法都封裝在此啊!
最近有個同學用到了RecyclerView跳轉到指定位置的需求,其實很簡單,在這里我就給出我的做法,有指教的和改進的歡迎提出,需要用的就用,勿噴哈…..
方法一,直接使用當前的manager
/**
* RecyclerView 移動到當前位置,
*
* @param manager 設置RecyclerView對應的manager
* @param n 要跳轉的位置
*/
public static void MoveToPosition(LinearLayoutManager manager, int n) {
manager.scrollToPositionWithOffset(n, 0);
manager.setStackFromEnd(true);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
方法二、根據當前RecyclerView的條目數量,這個相對復雜一些,但是可以有效地避免指針越界呦..
/**
* RecyclerView 移動到當前位置,
*
* @param manager 設置RecyclerView對應的manager
* @param mRecyclerView 當前的RecyclerView
* @param n 要跳轉的位置
*/
public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {
int firstItem = manager.findFirstVisibleItemPosition();
int lastItem = manager.findLastVisibleItemPosition();
if (n <= firstItem) {
mRecyclerView.scrollToPosition(n);
} else if (n <= lastItem) {
int top = mRecyclerView.getChildAt(n - firstItem).getTop();
mRecyclerView.scrollBy(0, top);
} else {
mRecyclerView.scrollToPosition(n);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 方法三:帶有滾動效果
/** * 目標項是否在最后一個可見項之后 */ private boolean mShouldScroll; /** * 記錄目標項位置 */ private int mToPosition; /** * 滑動到指定位置 * * @param mRecyclerView * @param position */ 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) { // 如果跳轉位置在第一個可見位置之前,就smoothScrollToPosition可以直接跳轉 mRecyclerView.smoothScrollToPosition(position); } else if (position <= lastItem) { // 跳轉位置在第一個可見項之后,最后一個可見項之前 // smoothScrollToPosition根本不會動,此時調用smoothScrollBy來滑動到指定位置 int movePosition = position - firstItem; if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) { int top = mRecyclerView.getChildAt(movePosition).getTop(); mRecyclerView.smoothScrollBy(0, top); } } else { // 如果要跳轉的位置在最后可見項之后,則先調用smoothScrollToPosition將要跳轉的位置滾動到可見位置 // 再通過onScrollStateChanged控制再次調用smoothMoveToPosition,執行上一個判斷中的方法 mRecyclerView.smoothScrollToPosition(position); mToPosition = position; mShouldScroll = true; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (mShouldScroll) { mShouldScroll = false; smoothMoveToPosition(mRecyclerView, mToPosition); } } });
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
if (TextUtils.equals(mCarBrandList.get(i).pinyin.charAt(0) + "", letter)) { if (i == 0) { // UIUtils.MoveToPosition(manager, mRecyclerView, i); smoothMoveToPosition(mRecyclerView,i); } else { smoothMoveToPosition(mRecyclerView,i+1); // UIUtils.MoveToPosition(manager, mRecyclerView, i + 1); } break; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
好了這樣就可以了,不信你試試…….