0

I am using RecyclerView with GridLayoutManager and The speed of RecyclerView Scroll is too much. I want it to slow down. I did try many other codes and approaches but, none worked for me.

Here is what I found

  1. How to make RecyclerView scroll smoothly?

This post suggests that In onCreateViewHolder and onBindViewHolder there shouldn't be I/O operations and taking too much time to execute any of these method creates a lag. But, I have no I/O operations and not much code into these.

...
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent, false);
        return new MyViewHolder(view,listner);
    }

  @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        GlideApp.with(context)
                .load(arrayList.get(position).getUri())
                .apply(RequestOptoins.overrideOf(180,180))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(holder.img);
        Log.d("FetchImages(): "," Glide Called");
    }
...
  1. How can I control the scrolling speed of recyclerView.smoothScrollToPosition(position)?

This post suggests to use a CustomLayoutManager Class. But, this didn't work for me either. May be because all answers are focused on LinearLayoutManager and may be it doesn't suupport GridLayoutManager.

  1. https://github.com/Azoft/CarouselLayoutManager/issues/79

I tried this link as well but, I can't understan what's going in there.

4.https://programmer.group/android-recycler-view-slides-quickly-to-the-top.html

Same with this link. Can't understand a thing.

List is too long to post here. But, Ultimately I tried everything and everycode I understood. But, so far nothing seems to work for me.

Note I am trying to show images from external storage. So, on first load glide will create thumbnails to cache and then on second load there will no larger size images. But, still there are more than 5000 images in my phone. And at a time recyclerview can show 40 images only. So, if I accidently scroll fast rest of the images will load in forground. So, I want to disable that fast scrolling so no matter how many photos are there recyclerview will only show loaded photos.

  • Don't use larger size image! – Elango Jun 19 '21 at 09:22
  • I am trying to show images from external storage. So, on first load glide will create thumbnails to cache and then on second load there will no larger size images. But, still there are more than 5000 images in my phone. And at a time recyclerview can show 40 images only. So, if I accidently scroll fast rest of the images will load in forground. So, I want to disable that fast scrolling so no matter how many photos are there recyclerview will only show loaded photos. – Anirudhdhsinh Jadeja Jun 19 '21 at 09:27
  • check this out , https://stackoverflow.com/a/26890631/12709358 – Elango Jun 19 '21 at 11:47

2 Answers2

1

You can create CustomRecyclerView and slow it down by overriding fling() method. You can read the official documentation here.

@Override
public boolean fling(int velocityX, int velocityY) {
     velocityY *= FLING_SPEED_FACTOR; // keep it less than 1.0 to slowdown
     return super.fling(velocityX, velocityY);
} 

This simply reduces the fling speed and even with same amount of fling your RecyclerView will scroll lesser.

Embydextrous
  • 1,611
  • 1
  • 12
  • 20
0

As @Embydextrousu suggested and gave the accurate solution. I want to add few things.

You have to change your RecyclerView in Xml as well to your customRecyclerView Class.

...
<com.example.project.CustomRecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />
...

Then You need to create a CustomRecyclerView class.

...
public class CustomRecyclerView  extends androidx.recyclerview.widget.RecyclerView {



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

    @Override
    public boolean fling(int velocityX, int velocityY) {
        velocityY *= 0.35; // keep it less than 1.0 to slowdown
        return super.fling(velocityX, velocityY);
    }
}
...

Where change the velocityY according to your need.

and finally implement in your fragment or Activity like this.

...
 CustomRecyclerView recyclerView;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = requireView().findViewById(R.id.recyclerview);
}
...

This will reduce the speed of recyclerView scrolling.