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