recycleview中让指定item在屏幕可视区域内

情景:

类似与歌词滚动 当用户拖动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

你可能感兴趣的:(recycleview中让指定item在屏幕可视区域内)