1 package com
.owncloud
.android
.ui
;
3 import android
.content
.Context
;
4 import android
.graphics
.Canvas
;
5 import android
.util
.AttributeSet
;
6 import android
.widget
.ListView
;
9 * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
11 * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
13 * @author David A. Velasco
15 public class ExtendedListView
extends ListView
{
17 private int mPositionToSetAndCenter
;
19 public ExtendedListView(Context context
) {
23 public ExtendedListView(Context context
, AttributeSet attrs
) {
24 super(context
, attrs
);
27 public ExtendedListView(Context context
, AttributeSet attrs
, int defStyle
) {
28 super(context
, attrs
, defStyle
);
37 protected void onDraw (Canvas canvas
) {
39 if (mPositionToSetAndCenter
> 0) {
40 this.setSelectionFromTop(mPositionToSetAndCenter
, getHeight() / 2);
41 mPositionToSetAndCenter
= 0;
46 * Public method to set the position of the item that should be centered in the visible area of the view.
48 * The position is saved here and checked in onDraw().
50 * @param position Position (in the list of items) of the item to center in the visible area.
52 public void setAndCenterSelection(int position
) {
53 mPositionToSetAndCenter
= position
;