情景:
类似与歌词滚动 当用户拖动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);
}
}
网友评论