How to determine the scroll direction of a ListView

I spent the whole day figuring out the scroll direction of the ListView , but couldn't find a solution. What I want to achieve is a movable ListView title, such as "Google Now", which increases as you scroll down and returns as soon as you scroll up.

What I have tried so far:

Implemented custom drag and drop ListView onScrollChanged , but it always gives 0 for all attributes.

Can anyone put me on the right track

enter image description here

+4
source share
2 answers

This is called the Quick Return pattern. Roman Nurik made a blog post explaining how he works and provided sample code for its implementation. You can find it here: https://plus.google.com/u/0/+RomanNurik/posts/1Sb549FvpJt

+4
source

Something like this should work. You just need to cache the old position and offset and use it to check the direction. You can also filter on scrollState:

 listView.setOnScrollListener(new OnScrollListener() { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { int position = listView.getFirstVisiblePosition(); View v = listView.getChildAt(0); int offset = (v == null) ? 0 : v.getTop(); if (mPosition < position || (mPosition == position && mOffset < offset) { // Scrolled up } else { // Scrolled down } mPosition = position; mOffset = offset; } }); 
0
source

Source: https://habr.com/ru/post/1442378/


All Articles