1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012 Bartek Przybylski 
   3  *   Copyright (C) 2012-2013 ownCloud Inc. 
   5  *   This program is free software: you can redistribute it and/or modify 
   6  *   it under the terms of the GNU General Public License version 2, 
   7  *   as published by the Free Software Foundation. 
   9  *   This program is distributed in the hope that it will be useful, 
  10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  12  *   GNU General Public License for more details. 
  14  *   You should have received a copy of the GNU General Public License 
  15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  19 package com
.owncloud
.android
.ui
; 
  21 import android
.content
.Context
; 
  22 import android
.graphics
.Canvas
; 
  23 import android
.util
.AttributeSet
; 
  24 import android
.widget
.ListView
; 
  27  * ListView allowing to specify the position of an item that should be centered in the visible area, if possible. 
  29  * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.  
  31  * @author David A. Velasco 
  33 public class ExtendedListView 
extends ListView 
{ 
  35     private int mPositionToSetAndCenter
; 
  37     public ExtendedListView(Context context
) { 
  41     public ExtendedListView(Context context
, AttributeSet attrs
) { 
  42         super(context
, attrs
); 
  45     public ExtendedListView(Context context
, AttributeSet attrs
, int defStyle
) { 
  46         super(context
, attrs
, defStyle
); 
  55     protected void onDraw (Canvas canvas
) { 
  57         if (mPositionToSetAndCenter 
> 0) { 
  58             this.setSelectionFromTop(mPositionToSetAndCenter
, getHeight() / 2); 
  59             mPositionToSetAndCenter 
= 0; 
  64      * Public method to set the position of the item that should be centered in the visible area of the view. 
  66      * The position is saved here and checked in onDraw(). 
  68      * @param position         Position (in the list of items) of the item to center in the visible area.      
  70     public void setAndCenterSelection(int position
) { 
  71         mPositionToSetAndCenter 
= position
;