android 如何以编程方式滚动recyclerView?

pdkcd3nj  于 2023-09-29  发布在  Android
关注(0)|答案(5)|浏览(135)

我有一个水平recyclerView,当我第一次打开Activity时,我想让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);

动画幻灯片左侧:

<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>

动画幻灯片右侧:

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

它的工作原理然而它只是幻灯片的recyclerview作为一个整体,我只是想滚动(幻灯片)的项目。我该怎么做?

f1tvaqid

f1tvaqid1#

可以使用scrollToPosition()

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调用上面的方法。但首先,您需要确保RecyclerView滚动到最后。因此,将ScrollListener放在RecyclerView上,以确保最后一项可见。

v6ylcynt

v6ylcynt2#

用这个。

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

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

crcmnpdw3#

如果你需要滚动项目回收视图到屏幕的中心时,为前。展开您的项目-使用这种方式:
在app/build.gradle中插入:

implementation 'com.andkulikov:transitionseverywhere:1.7.4'

当hadle点击适配器的onBindViewHolder- insert:

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);
dly7yett

dly7yett4#

尝试其他事情后不工作?
这段出色的代码对我很有效

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);
            }
        }
    });
}

如果想跳到其他列表项&而不是last,可以修改行
final int lastItemPosition = adapter.getItemCount() - 1;
Source

ss2ws0br

ss2ws0br5#

如果有人在2023年寻找这个解决方案,这里有一种方法可以使用协程在Activity中实现滚动到某个索引。如果没有应用足够的延迟,无法正常工作的原因是,将列表更新到适配器和滚动回收视图的操作将并行进行,因此无法保证在将列表提交到适配器后进行滚动。

adapter.submitList(uiState.yourList) // using ListAdapter for better perfomance
 lifecycleScope.launch {
    delay(500)
    binding.rvNews.smoothScrollToPosition(INDEX_VALUE) // scrolls to the top for Index value 0
}

相关问题