美文网首页
recycleview中让指定item在屏幕可视区域内

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

作者: 最爱平角裤 | 来源:发表于2016-10-31 14:12 被阅读394次

    情景:

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

    相关文章

      网友评论

          本文标题:recycleview中让指定item在屏幕可视区域内

          本文链接:https://www.haomeiwen.com/subject/bbkputtx.html