Moved some classes from root package to .utils
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ExtendedListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.ui.fragment;
20
21 import com.actionbarsherlock.app.SherlockFragment;
22 import com.owncloud.android.R;
23 import com.owncloud.android.ui.ExtendedListView;
24 import com.owncloud.android.utils.Log_OC;
25
26
27 import android.os.Bundle;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import android.widget.ListAdapter;
33 import android.widget.AdapterView.OnItemClickListener;
34 import android.widget.ListView;
35
36 /**
37 * TODO extending SherlockListFragment instead of SherlockFragment
38 */
39 public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener {
40
41 private static final String TAG = ExtendedListFragment.class.getSimpleName();
42
43 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
44
45 protected ExtendedListView mList;
46
47 public void setListAdapter(ListAdapter listAdapter) {
48 mList.setAdapter(listAdapter);
49 mList.invalidate();
50 }
51
52 public ListView getListView() {
53 return mList;
54 }
55
56
57 @Override
58 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
59 Log_OC.e(TAG, "onCreateView");
60 //mList = new ExtendedListView(getActivity());
61 View v = inflater.inflate(R.layout.list_fragment, null);
62 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
63 mList.setOnItemClickListener(this);
64 //mList.setEmptyView(v.findViewById(R.id.empty_list_view)); // looks like it's not a cool idea
65 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
66 mList.setDividerHeight(1);
67
68 if (savedInstanceState != null) {
69 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
70 setReferencePosition(referencePosition);
71 }
72
73 return v;
74 }
75
76
77 @Override
78 public void onSaveInstanceState(Bundle savedInstanceState) {
79 super.onSaveInstanceState(savedInstanceState);
80 Log_OC.e(TAG, "onSaveInstanceState()");
81 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
82 }
83
84
85 /**
86 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
87 * the device is turned to other position.
88 *
89 * THe current policy is take as a reference the visible item in the center of the screen.
90 *
91 * @return The position in the list of the visible item in the center of the screen.
92 */
93 protected int getReferencePosition() {
94 if (mList != null) {
95 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
96 } else {
97 return 0;
98 }
99 }
100
101
102 /**
103 * Sets the visible part of the list from the reference position.
104 *
105 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
106 */
107 protected void setReferencePosition(int position) {
108 if (mList != null) {
109 mList.setAndCenterSelection(position);
110 }
111 }
112
113 @Override
114 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
115 // to be @overriden
116 }
117
118
119 }