在android中,我如何獲得listview的滾動位置?Android獲取滾動位置的listview填充不同的高度
我知道,我可以檢索均勻填充列表視圖的滾動位置用下面的代碼:
int scrollY = -this.getChildAt(0).getTop() + this.getFirstVisiblePosition()* this.getChildAt(0).getHeight();
的代碼假定孩子(項目)的所有高度在ListView等於(this.getChildAt(0).getHeight()
)
現在,如果我填充我的listview與不等大小的項目,我如何得到正確的滾動位置?
我的列表視圖看起來是這樣的:
這就是爲什麼我需要滾動位置:
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;
}
我需要繪製下面的列表視圖的滾動一個指標
我會援引它像
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas = drawIndicator(canvas);
super.onDraw(canvas);
canvas.restore();
}
你能解釋一下爲什麼你需要滾動位置? –
請檢查的問題,我已經加入然而之所以 – tom91136