Scroll ListView by Pixel in Android

I want to scroll a ListView in Android by the number of pixels. For example, I want to scroll down the list 10 pixels (so that the first element in the list hides the first 10 pixels).

I thought the explicitly visible scrollBy or scrollTo in the ListView would do the job, but they didn’t, instead they scroll the whole list incorrectly (in fact, getScrollY always returns zero, even if I scroll through the list using my finger.)

What I'm doing is capturing trackball events, and I want to smoothly scroll through the list according to the movement of the trackball.

+7
android listview scroll
source share
5 answers

The supported way to scroll a ListView widget:

mListView.smoothScrollToPosition(position);

http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)

However, since you specifically mentioned that you would like to compensate for the vertical representation, you should call:

mListView.setSelectionFromTop(position, yOffset);

http://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int,%20int)

Note that you can also use smoothScrollByOffset(yOffset) . However, it is only supported by API> = 11

http://developer.android.com/reference/android/widget/ListView.html#smoothScrollByOffset(int)

+10
source share

If you look at the source code for the scrollListBy () method added in api 19 you will see that you can use the scope package of the trackMotionScroll method.

 public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @param y the amount of pixels to scroll by vertically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclaredMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } 
+10
source share

Here is the code from my subclass of ListView . It can be easily adapted so that it can be used in the opcode.

getListItemsHeight() returns the total pixel height of the list and fills the array with vertical pixel offsets of each element. Although this information is valid, getListScrollY() returns the current scroll position of the vertical pixel, and scrollListToY() scrolls the list to the position of the pixel. If the size or contents of the list changes, you must call getListItemsHeight() again.

 private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } 
+3
source share

ListViewCompat is now the best solution.

 android.support.v4.widget.ListViewCompat.scrollListBy(@NonNull ListView listView, int y) 
+1
source share

if you want to move around the pixels then you can use this

 public void scrollBy(ListView l, int px){ l.setSelectionFromTop(l.getFirstVisiblePosition(),l.getChildAt(0).getTop() - px); } 

this works for even with massive headers

0
source share

All Articles