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