2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2012 Bartek Przybylski
6 * Copyright (C) 2012-2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
;
24 import android
.content
.Context
;
25 import android
.graphics
.Canvas
;
26 import android
.util
.AttributeSet
;
27 import android
.widget
.ListView
;
29 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
32 * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
34 * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
36 public class ExtendedListView
extends ListView
{
38 private static final String TAG
= ExtendedListView
.class.getSimpleName();
40 private int mPositionToSetAndCenter
= 0;
42 public ExtendedListView(Context context
) {
46 public ExtendedListView(Context context
, AttributeSet attrs
) {
47 super(context
, attrs
);
50 public ExtendedListView(Context context
, AttributeSet attrs
, int defStyle
) {
51 super(context
, attrs
, defStyle
);
60 protected void onDraw (Canvas canvas
) {
62 if (mPositionToSetAndCenter
> 0) {
63 Log_OC
.v(TAG
, "Centering around position " + mPositionToSetAndCenter
);
64 this.setSelectionFromTop(mPositionToSetAndCenter
, getHeight() / 2);
65 mPositionToSetAndCenter
= 0;
70 * Public method to set the position of the item that should be centered in the visible area of the view.
72 * The position is saved here and checked in onDraw().
74 * @param position Position (in the list of items) of the item to center in the visible area.
76 public void setAndCenterSelection(int position
) {
77 mPositionToSetAndCenter
= position
;