Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between a Drag action and a Fling action in recyclerview

Recyclerview right now has 3 states.

SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING

An issue raised for the same to include a fling state.I cannot determine if anything was done regarding this.

Is there a way to differentiate between a Drag and a Fling in recyclerview.

EDIT: The requirement for such functionality: When the user flings,I want to be able to pause loading images(all images are url requests) in the recycler view and resume once he reaches an item of interest,thereby ensuring that the image he is currently viewing loads before the others.

like image 527
Droidekas Avatar asked Feb 04 '16 07:02

Droidekas


1 Answers

SCROLL_STATE_FLING : No longer a part of RecyclerView as it is not mentioned in documentation here

Regarding your requirement :

RecyclerView is inside android.view.ViewGroup and as per its source code it extends ViewGroup documentation here .

The scrolling in a RecyclerView is sort of split up between the RecyclerView and the LinearLayoutManager. There are two cases that needed to handle:

  1. The user flings the view. The default behavior is that the RecyclerView passes the fling to an internal Scroller which then performs the scrolling magic. This is problematic because then the RecyclerView usually settles in an unsnapped position. Solve this by overriding the RecyclerView fling() implementation and instead of flinging, smoothscroll the LinearLayoutManager to a position.
  2. The user lifts their finger with insufficient velocity to initiate a scroll. No fling occurs in this case. If you want to detect this case in the event that the view is not in a snapped position, you can do this by overriding the onTouchEvent method.

See here for detail Snappy scrolling in RecyclerView

A few ViewPager hints that are worth mentioning as RecyclerView is a child of it:

  1. Think about modifying the number of pages that are cached. This is especially important when you only have 3 or 4 pages. The default setting will store 1 page either side of the current page. In the scenario that you have 3 pages, swiping to the middle page will mean that all of your pages will be cached. Then swiping to the first or last page will drop one of the pages out of memory and they will need to be recreated and re-added when you swipe back again. By setting setOffscreenPageLimit(2) you’ll allow all of your pages to stay in memory all the time. This is a trade off between performance and memory considerations, so it is a good idea to listen for low memory warnings and be prepared to remove edge pages if necessary.

  2. If you’re trying to replace Views in your ViewPager, it isn’t enough just to change the data set behind the adapter and call notifyDataSetChanged(). You also need to ensure that you’ve correctly implemented getItemPosition(Object object) and return POSITION_NONE for items that have changed and return POSITION_UNCHANGED or the actual position for items that haven’t changed.

  3. Another API that was added is setPageMargin() and setPageMarginDrawable(), allowing you to easily separate your pages.

See here for detail Horizontal View Swiping with ViewPager, Updated

Difference between a Drag and a Fling

For Drag feature You can use some of the RecyclerView's companion classes:

  1. ItemTouchHelper, which is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.

  2. its ItemTouchHelper.Callback, which is the contract between ItemTouchHelper and your application

For fling you can see

  1. Android fling actions on a RecyclerView

  2. Android : Control Smooth scroll over recycler view

like image 88
Mohammad Tauqir Avatar answered Sep 29 '22 11:09

Mohammad Tauqir