Control Smooth Scroll over Recycler View

In this post I will show you how can you achieve smooth scroll over RecyclerView

The key to a smoothly scrolling RecyclerView is to keep the application’s main thread (the UI thread) free from heavy processing. Recycler view is a flexible view for providing a limited window into a large data set.

Making Listview smooth there are many examples but none for the RecyclerView. Sometime its happen that user fling the screen so fast that RecyclerView looks shaky. It leave bad impression on the user.

For smooth scroll we need to control fling of the user. I have made custom class that extends RecyclerView. This can be achieve in just 3 steps.

Step 1 :

Create CustomRecyclerView Class. Below class is particular for Vertical RecyclerView.


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

public class CustomRecyclerView extends RecyclerView {

    Context context;

    public CustomRecyclerView(Context context) {
        super(context);
        this.context = context;
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean fling(int velocityX, int velocityY) {
        velocityY *= 0.7;
        return super.fling(velocityX, velocityY);
    }
}

For Horizontal RecyclerView just change fling method.


@Override
public boolean fling(int velocityX, int velocityY) {
    velocityX *= 0.7;
    return super.fling(velocityX, velocityY);
}

Step 2:

Because of we are using custom class for the RecyclerView we need to use class name with package name in the XML layout as well.


<yourpackage.CustomRecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

For example your create CustomRecyclerView under com.sid.views then in XML it will be com.sid.views.CustomRecyclerView

Step 3:

And the last step for reading custom class in the java. We need to use like that.


CustomRecyclerView mRecyclerView;
mRecyclerView = (CustomRecyclerView) findViewById(R.id.my_recycler_view);

Get complete class from Github. For more information regarding smooth scroll go to this Question.

Thank you for Reading

 

Advertisements

One thought on “Control Smooth Scroll over Recycler View

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s