Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid get scroll position of listview populated with varied height
    text
    copied!<p>In android, how do i get the scroll position of the listview?</p> <p>I know that I can retrieve the scroll position of a uniform populated listview with the following code:</p> <pre><code>int scrollY = -this.getChildAt(0).getTop() + this.getFirstVisiblePosition()* this.getChildAt(0).getHeight(); </code></pre> <p>the code assumes all heights of children(item) in the listview to be equal(<code>this.getChildAt(0).getHeight()</code>)</p> <p>Now, if i populate my listview with not equally sized items, how do i get the proper scroll position?</p> <p>My listview looks something like this: <img src="https://i.stack.imgur.com/ugKwN.png" alt="enter image description here"></p> <p>This is why I need the scroll position:</p> <pre><code>private Canvas drawIndicator(Canvas canvas) { int scrollY = getCurrentScrollPosition(); paint.setColor(Color.GRAY); paint.setAlpha(100); canvas.drawRect(getLeft(), indicatorPosition[0] - scrollY, getRight(), indicatorPosition[1] - scrollY, paint); //Log.d(VIEW_LOG_TAG, "drawIndicator:" + (indicatorPosition[1] - //scrollY)); paint.setColor(Color.parseColor("#47B3EA")); canvas.drawRect(getLeft(), indicatorPosition[1] - scrollY - (indicatorHeight / 2), getRight(), indicatorPosition[1] - scrollY + indicatorHeight, paint); return canvas; } </code></pre> <p>i need to draw a indicator that follows the scroll of the listview</p> <p>i would invoke it like </p> <pre><code>@Override protected void onDraw(Canvas canvas) { canvas.save(); canvas = drawIndicator(canvas); super.onDraw(canvas); canvas.restore(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload