如何以编程方式滚动recyclerView?

时间:2018-02-19 09:10:07

标签: android android-layout android-recyclerview

我有一个水平的recyclerView,当我第一次打开活动时,我想让recyclerview中的所有项目滚动到底部(在这种情况下向右)并返回到顶部(向左)。有点像动画。滚动行为应该对用户可见。

我试着这样做:

Animation slideRight = AnimationUtils.loadAnimation(this, R.anim.slide_right);
        Animation slideLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left);
        slideRight.setDuration(1000);
        slideLeft.setDuration(1000);
        slideRight.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                recyclerView.startAnimation(slideLeft);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        recyclerView.startAnimation(slideRight);

anim slide left:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="200"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>

anim slide right:

<translate
    android:duration="200"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

它可以工作,但它只是将recyclerview作为一个整体滑动,我只想滚动(滑动)项目。我怎么能这样做?

4 个答案:

答案 0 :(得分:11)

您可以使用scrollTo()

  recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.scrollToPosition(adapter.getItemCount() - 1);
            // Here adapter.getItemCount()== child count
        }
    });

smoothScrollToPosition()

recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
        }
    });

要再次向上移动,您需要使用索引0调用上面的方法。但首先,您需要确保RecyalerView滚动到最后。因此,在ScrollListener上添加RecyclerView以确保最后一项是可见的。

答案 1 :(得分:1)

例如当您需要将recyclerview的滚动项滚动到屏幕的CENTER时。扩展项目-使用这种方式:

在app / build.gradle中-插入:

 implementation 'com.andkulikov:transitionseverywhere:1.7.4'

当hadle单击适配器的onBindViewHolder时-插入:

AutoTransition transitionPort = new AutoTransition();
                transitionPort.setDuration(100);
                transitionPort.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(@NonNull Transition transition) {
                        recyclerView.setEnabled(false);
                        recyclerView.setClickable(false);
                    }

                    @Override
                    public void onTransitionEnd(@NonNull Transition transition) {
                        recyclerView.post(() -> recyclerView.smoothScrollToPosition(position));
                        recyclerView.setEnabled(true);
                        recyclerView.setClickable(true);
                    }

                    @Override
                    public void onTransitionCancel(@NonNull Transition transition) {
                        recyclerView.setEnabled(true);
                        recyclerView.setClickable(true);
                    }

                    @Override
                    public void onTransitionPause(@NonNull Transition transition) {

                    }

                    @Override
                    public void onTransitionResume(@NonNull Transition transition) {

                    }
                });
                TransitionManager.beginDelayedTransition(recyclerView, transitionPort);

答案 2 :(得分:0)

使用此..

 int top = 0;
 recyclerView.smoothScrollToPosition(top); // for top

 int bottom = recyclerView.getAdapter().getItemCount()-1;
 recyclerView.smoothScrollToPosition(bottom);

答案 3 :(得分:0)

尝试其他方法后无法正常工作吗?

这段出色的代码对我有用

private void scrollToBottom(final RecyclerView recyclerView) {
    // scroll to last item to get the view of last item
    final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    final RecyclerView.Adapter adapter = recyclerView.getAdapter();
    final int lastItemPosition = adapter.getItemCount() - 1;

    layoutManager.scrollToPositionWithOffset(lastItemPosition, 0);
    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            // then scroll to specific offset
            View target = layoutManager.findViewByPosition(lastItemPosition);
            if (target != null) {
                int offset = recyclerView.getMeasuredHeight() - target.getMeasuredHeight();
                layoutManager.scrollToPositionWithOffset(lastItemPosition, offset);
            }
        }
    });
}

如果您想跳到其他列表项而不是最后一个,可以修改行

final int lastItemPosition = adapter.getItemCount() - 1;

Source