recyclerview滚动到指定条目

android recyclerview滚动到指定条目

自从android5.0推出RecyclerView以后,RecyclerView越来越受广大程序员的热爱了!大家都知道RecyclerView的出现目的是为了替代listview和ScrollView在列表方面的使用!那么listview和ScrollView的所有功能和方法都应该有的!

但是RecyclerView的很多方法,不是封装在RecyclerView中的,当我们在RecyclerView中找不到对应的方法时,就应该想到他的管理类manager了!
大多方法都封装在此啊!

      /**
         * RecyclerView 移动到当前位置,
         * 方法一,直接使用recycleview自身的manager的方法,需要处理下标越界的问题
         *
         * @param manager 设置RecyclerView对应的manager
         * @param n       要跳转的位置
         */
        public static void MoveToPosition(LinearLayoutManager manager, int n) {
            try {   //避免空指针越界,捕获异常
                manager.scrollToPositionWithOffset(n, 0);
                manager.setStackFromEnd(true);
            } catch (Exception e) {

            }
        }


        /**
         * RecyclerView 移动到当前位置,
         *
         * @param manager       设置RecyclerView对应的manager
         * @param mRecyclerView 当前的RecyclerView
         * @param n             要跳转的位置
         */
        public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {


            int firstItem = manager.findFirstVisibleItemPosition();
            int lastItem = manager.findLastVisibleItemPosition();
            if (n <= firstItem) {
                mRecyclerView.scrollToPosition(n);
            } else if (n <= lastItem) {
                int top = mRecyclerView.getChildAt(n - firstItem).getTop();
                mRecyclerView.scrollBy(0, top);
            } else {
                mRecyclerView.scrollToPosition(n);
            }
        }


        ///////////////////////////////////////////////////方法三平滑滚动到指定的位置
        /**
         * 目标项是否在最后一个可见项之后
         */
        private boolean mShouldScroll;
        /**
         * 记录目标项位置
         */
        private int mToPosition;

        /**
         * 滑动到指定位置
         *
         * @param mRecyclerView
         * @param position
         */
        private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
            // 第一个可见位置
            int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
            // 最后一个可见位置
            int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));

            if (position < firstItem) {
                // 如果跳转位置在第一个可见位置之前,就smoothScrollToPosition可以直接跳转
                mRecyclerView.smoothScrollToPosition(position);
            } else if (position <= lastItem) {
                // 跳转位置在第一个可见项之后,最后一个可见项之前
                // smoothScrollToPosition根本不会动,此时调用smoothScrollBy来滑动到指定位置
                int movePosition = position - firstItem;
                if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
                    int top = mRecyclerView.getChildAt(movePosition).getTop();
                    mRecyclerView.smoothScrollBy(0, top);
                }
            } else {
                // 如果要跳转的位置在最后可见项之后,则先调用smoothScrollToPosition将要跳转的位置滚动到可见位置
                // 再通过onScrollStateChanged控制再次调用smoothMoveToPosition,执行上一个判断中的方法
                mRecyclerView.smoothScrollToPosition(position);
                mToPosition = position;
                mShouldScroll = true;
            }
        }


        //使用
         /*       mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mShouldScroll) {
                    mShouldScroll = false;
                    smoothMoveToPosition(mRecyclerView, mToPosition);
                }
            }
        });

                if (TextUtils.equals(mCarBrandList.get(i).pinyin.charAt(0) + "", letter)) {
            if (i == 0) {
    //                            UIUtils.MoveToPosition(manager, mRecyclerView, i);
                smoothMoveToPosition(mRecyclerView,i);
            } else {
                smoothMoveToPosition(mRecyclerView,i+1);
    //                            UIUtils.MoveToPosition(manager, mRecyclerView, i + 1);
            }
            break;
        }

    */

猜你喜欢

转载自blog.csdn.net/bencheng06/article/details/79140262