情景:
類似與歌詞滾動 當用戶拖動recycle后,在下一句時讓當前句重新回到視野
實現:
recycleChallenge.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
// 當不滾動時
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//獲取最后一個完全顯示的ItemPosition
lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
isScroll = true;
System.out.println();
}
}
});
if(isScroll&&Math.abs(lastVisibleItem-currWord)>4){
recycleChallenge.smoothScrollToPosition(currWord);
isScroll = false;
}else{
if(currWord+2<adapter.getItemCount()){
recycleChallenge.smoothScrollToPosition(currWord+2);
}else{
recycleChallenge.smoothScrollToPosition(currWord+1);
}
}