Android: Listview.Getscrolly() - Does It Work

Android: ListView.getScrollY() - does it work?

getScrollY() is actually a method on View, not ListView. It is referring to the scroll amount of the entire view, so it will almost always be 0.

If you want to know how far the ListView's contents are scrolled, you can use listView.getFirstVisiblePosition();

Android ListView current scroll location Y pixels

There is no notion of Y scroll for a ListView in Android simply because the total height of the content is unknown. Only the height of the displayed content is known.

However it is possible to get the current position/Y scroll of a visible item using the following hack:

getListView().getChildAt(0).getTop();

Android getting exact scroll position in ListView

Okay, I found a workaround, using the following code:

View c = listview.getChildAt(0);
int scrolly = -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();

The way it works is it takes the actual offset of the first visible list item and calculates how far it is from the top of the view to determine how much we are "scrolled into" the view, so now that we know that we can calculate the rest using the regular getFirstVisiblePosition method.

Determine if the list view is about to stop scrolling?

You can set an OnScrollListener, and then store the value from absListView.getScrollY() each sample and compare it the previous sample to compute the velocity of the scroll. Once that drops below a threshold you define, you can take over scrolling.

Android listView find the amount of pixels scrolled

I had the same problem.

I cannot use View.getScrollY() because it always returns 0 and I cannot use OnScrollListener.onScroll(...) because it works with positions not with pixels. I cannot subclass ListView and override onScrollChanged(...) because its parameter values are always 0. Meh.

All I want to know is the amount the children (i.e. content of listview) got scrolled up or down. So I came up with a solution. I track one of the children (or you can say one of the "rows") and follow its vertical position change.

Here is the code:

public class ObservableListView extends ListView {

public static interface ListViewObserver {
public void onScroll(float deltaY);
}

private ListViewObserver mObserver;
private View mTrackedChild;
private int mTrackedChildPrevPosition;
private int mTrackedChildPrevTop;

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

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mTrackedChild == null) {
if (getChildCount() > 0) {
mTrackedChild = getChildInTheMiddle();
mTrackedChildPrevTop = mTrackedChild.getTop();
mTrackedChildPrevPosition = getPositionForView(mTrackedChild);
}
} else {
boolean childIsSafeToTrack = mTrackedChild.getParent() == this && getPositionForView(mTrackedChild) == mTrackedChildPrevPosition;
if (childIsSafeToTrack) {
int top = mTrackedChild.getTop();
if (mObserver != null) {
float deltaY = top - mTrackedChildPrevTop;
mObserver.onScroll(deltaY);
}
mTrackedChildPrevTop = top;
} else {
mTrackedChild = null;
}
}
}

private View getChildInTheMiddle() {
return getChildAt(getChildCount() / 2);
}

public void setObserver(ListViewObserver observer) {
mObserver = observer;
}

}

Couple of notes:

  • we override onScrollChanged(...) because it gets called when the listview is scrolled (just its parameters are useless)
  • then we choose a child (row) from the middle (doesn't have to be precisely the child in the middle)
  • every time scrolling happens we calculate vertical movement based on previous position (getTop()) of tracked child
  • we stop tracking a child when it is not safe to be tracked (e.g. in cases where it might got reused)

how does scrolling in android listview work?

I believe you want:

listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);


Related Topics



Leave a reply



Submit