Merge branch 'develop' into share_password_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / ExtendedListView.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2012 Bartek Przybylski
6 * Copyright (C) 2012-2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.ui;
23
24 import android.content.Context;
25 import android.graphics.Canvas;
26 import android.util.AttributeSet;
27 import android.widget.ListView;
28
29 import com.owncloud.android.lib.common.utils.Log_OC;
30
31 /**
32 * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
33 *
34 * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
35 */
36 public class ExtendedListView extends ListView {
37
38 private static final String TAG = ExtendedListView.class.getSimpleName();
39
40 private int mPositionToSetAndCenter = 0;
41
42 public ExtendedListView(Context context) {
43 super(context);
44 }
45
46 public ExtendedListView(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 }
49
50 public ExtendedListView(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
52 }
53
54 /**
55 * {@inheritDoc}
56 *
57 *
58 */
59 @Override
60 protected void onDraw (Canvas canvas) {
61 super.onDraw(canvas);
62 if (mPositionToSetAndCenter > 0) {
63 Log_OC.v(TAG, "Centering around position " + mPositionToSetAndCenter);
64 this.setSelectionFromTop(mPositionToSetAndCenter, getHeight() / 2);
65 mPositionToSetAndCenter = 0;
66 }
67 }
68
69 /**
70 * Public method to set the position of the item that should be centered in the visible area of the view.
71 *
72 * The position is saved here and checked in onDraw().
73 *
74 * @param position Position (in the list of items) of the item to center in the visible area.
75 */
76 public void setAndCenterSelection(int position) {
77 mPositionToSetAndCenter = position;
78 }
79
80 }