RecyclerView滑動到指定位置


轉:https://www.dazhuanlan.com/2019/12/16/5df6e27c71058/?__cf_chl_jschl_tk__=45b506af0a5845c229f6bcff22c07e3a50fbcabf-1609382170-0-AX6zhXvb63rOcwTNx--VLk8gWlC7KLxJkxoJdhyzcFpu2qKoP5D-4E7Ifwm2q_frwWyoZz2Tc8K2oyBtZ6sqlllnkw4C6BqmQBUKz8nVVrLNRryyAQtMa8N1dw1AvS9F7vrLz8XexzGHXccl-eiFfEu9E7n26HIPwl67cCq-W2jdrhwR-ZO392s5oQod9EO1KeiwurDvsyrJJiTbq7eBZ7MGKk5sLXicXfav_alHx_EM_2rU61WAuxYRBAGgjjwV0bcuElklPvcLTrNYZNDMgvmhVGRnVxgS8cekVmATdZkTjTEqEwObgAktShJxTfSHp-4xWWKMWgTyf8qgAJYd-7Y

 

前言

有個項目,需要定位recyclerView到某個item。
一般用 mRecycleview.smoothScrollToPosition(0) 滑動到頂部,具有滾動效果。
但是如果我們想滾動到任意指定位置,那么smoothScrollToPosition()
就不能保證所指定item位於屏幕頂部,以下參照網上的一些帖子做一下收藏整合:

第一種

能實現指定位置位於屏幕頂部,但是不具有平滑滾動視覺效果

1
2
3
4
5
if (position != -1) {
mRecycleview.scrollToPosition(position);
LinearLayoutManager mLayoutManager = (LinearLayoutManager) mRecycleview.getLayoutManager();
mLayoutManager.scrollToPositionWithOffset(position, 0);
}

第二種

能實現指定位置位於屏幕頂部,具有平滑滾動視覺效果

  1. 如果如果跳轉位置在第一個可見位置之前,就smoothScrollToPosition()可以直接跳轉;
  2. 如果跳轉位置在第一個可見項之后,最后一個可見項之前smoothScrollToPosition()不會滾動,調用smoothScrollBy來滑動到指定位置;
  3. 如果要跳轉的位置在最后可見項之后,則先調用smoothScrollToPosition()將要跳轉的位置滾動到可見位置,在addOnScrollListener()里通過onScrollStateChanged控制,調用smoothMoveToPosition,再次執行判斷;
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
41
42
43
44
45
46
47
48
private boolean mShouldScroll; 

private int mToPosition; //記錄目標項位置
/**
* 滑動到指定位置
*/
private void (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;
}
}

//在onCreate中設置滾動監聽事件
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (mShouldScroll&& RecyclerView.SCROLL_STATE_IDLE == newState) {
mShouldScroll = false;
smoothMoveToPosition(irc, mToPosition);
}
}
});

//最后在具體業務中,設置
if (position != -1) {
smoothMoveToPosition(irc,position);
}else {
smoothMoveToPosition(irc,position+1);
}

特殊情況

另一種簡單粗暴的方案:項目里有要求滾動到不可視的item,而在recycler是有Holder機制,沒有顯示不加載,需要先滾動到相對應的holder,等在加載完所有的holder之后,在通過adapter設置item的內容

1
2
3
4
5
6
7
8
9
10
11
12
//具體業務,
recyclerView.scrollToPosition(curPosition);
LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
mLayoutManager.scrollToPositionWithOffset(curPosition, 0);

// 延遲一秒,原因:在recycler的Holder機制
new Handler().postDelayed(new Runnable() {

public void run() {
adapter.setLocation(curPosition);
}
}, 1000);

參考鏈接

  1. RecyclerView滑動到指定位置,並指定位置在頂部


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM