Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll recyclerView programmatically?

I have a horizontal recyclerView, When I first open the activity I want to make all the items in recyclerview scroll to the bottom (to the right in this case) and back to the top (to the left). Kinda like an animation. Scroll behavior should be visible to the user.

I tried to do it like:

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%" />

it works however it just slides the recyclerview as a whole, I just want to scroll(slide) the items. How can I do this?

like image 489
Ege Kuzubasioglu Avatar asked Feb 19 '18 09:02

Ege Kuzubasioglu


People also ask

How do I scroll end of RecyclerView programmatically?

You can use scrollToPosition() with the index of the last position. Based on the doc, " RecyclerView does not implement scrolling logic, rather forwards the call to scrollToPosition(int)".

How do I scroll to the bottom of my recycler view?

Recyclerview scroll to bottom using scrollToPositon. After setting the adapter, then call the scrollToPosition function to scroll the recycler view to the bottom.


3 Answers

You can use scrollToPosition()

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

or smoothScrollToPosition()

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

To move up again, you need to call the above method with index 0. But first, you need to make sure that the RecyclerView is scrolled to last. So, put a ScrollListener on RecyclerView to make sure the last item is visible.

like image 70
ADM Avatar answered Oct 06 '22 15:10

ADM


Use this..

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

 int bottom = recyclerView.getAdapter().getItemCount()-1;
 recyclerView.smoothScrollToPosition(bottom);
like image 24
Santanu Sur Avatar answered Oct 06 '22 16:10

Santanu Sur


if you need scroll item of recyclerview to CENTER of screen when for ex. expand your item - use this way:

in app/build.gradle - insert:

 implementation 'com.andkulikov:transitionseverywhere:1.7.4'

when hadle click in onBindViewHolder of adapter - 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);
like image 23
nicolas asinovich Avatar answered Oct 06 '22 14:10

nicolas asinovich