Keep the same item in the CENTER of the files list when the device is turned to other...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / ExtendedListView.java
1 package com.owncloud.android.ui;
2
3 import android.content.Context;
4 import android.graphics.Canvas;
5 import android.util.AttributeSet;
6 import android.widget.ListView;
7
8 /**
9 * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
10 *
11 * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
12 *
13 * @author David A. Velasco
14 */
15 public class ExtendedListView extends ListView {
16
17 private int mPositionToSetAndCenter;
18
19 public ExtendedListView(Context context) {
20 super(context);
21 }
22
23 public ExtendedListView(Context context, AttributeSet attrs) {
24 super(context, attrs);
25 }
26
27 public ExtendedListView(Context context, AttributeSet attrs, int defStyle) {
28 super(context, attrs, defStyle);
29 }
30
31 /**
32 * {@inheritDoc}
33 *
34 *
35 */
36 @Override
37 protected void onDraw (Canvas canvas) {
38 super.onDraw(canvas);
39 if (mPositionToSetAndCenter > 0) {
40 this.setSelectionFromTop(mPositionToSetAndCenter, getHeight() / 2);
41 mPositionToSetAndCenter = 0;
42 }
43 }
44
45 /**
46 * Public method to set the position of the item that should be centered in the visible area of the view.
47 *
48 * The position is saved here and checked in onDraw().
49 *
50 * @param position Position (in the list of items) of the item to center in the visible area.
51 */
52 public void setAndCenterSelection(int position) {
53 mPositionToSetAndCenter = position;
54 }
55 }