8a017b5498ec7a4714abc0a81a67eca7706fee05
[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.support.v4.widget.SwipeRefreshLayout;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.ListAdapter;
34 import android.widget.AdapterView.OnItemClickListener;
35 import android.widget.ListView;
36
37 /**
38 * TODO extending SherlockListFragment instead of SherlockFragment
39 */
40 public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
41
42 private static final String TAG = ExtendedListFragment.class.getSimpleName();
43
44 private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
45
46 protected ExtendedListView mList;
47
48 private SwipeRefreshLayout mRefreshLayout;
49
50 public void setListAdapter(ListAdapter listAdapter) {
51 mList.setAdapter(listAdapter);
52 mList.invalidate();
53 }
54
55 public ListView getListView() {
56 return mList;
57 }
58
59
60 @Override
61 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
62 Log_OC.e(TAG, "onCreateView");
63 //mList = new ExtendedListView(getActivity());
64
65 View v = inflater.inflate(R.layout.list_fragment, null);
66 mList = (ExtendedListView)(v.findViewById(R.id.list_root));
67 mList.setOnItemClickListener(this);
68 //mList.setEmptyView(v.findViewById(R.id.empty_list_view)); // looks like it's not a cool idea
69 mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
70 mList.setDividerHeight(1);
71
72 if (savedInstanceState != null) {
73 int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
74 setReferencePosition(referencePosition);
75 }
76
77 // Pull down refresh
78 mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
79 mRefreshLayout.setOnRefreshListener(this);
80
81 return v;
82 }
83
84
85 @Override
86 public void onSaveInstanceState(Bundle savedInstanceState) {
87 super.onSaveInstanceState(savedInstanceState);
88 Log_OC.e(TAG, "onSaveInstanceState()");
89 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
90 }
91
92
93 /**
94 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
95 * the device is turned to other position.
96 *
97 * THe current policy is take as a reference the visible item in the center of the screen.
98 *
99 * @return The position in the list of the visible item in the center of the screen.
100 */
101 protected int getReferencePosition() {
102 if (mList != null) {
103 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
104 } else {
105 return 0;
106 }
107 }
108
109
110 /**
111 * Sets the visible part of the list from the reference position.
112 *
113 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
114 */
115 protected void setReferencePosition(int position) {
116 if (mList != null) {
117 mList.setAndCenterSelection(position);
118 }
119 }
120
121 @Override
122 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
123 // to be @overriden
124 }
125
126 @Override
127 public void onRefresh() {
128 // to be @overriden
129 mRefreshLayout.setRefreshing(false);
130 }
131
132 /**
133 * Enables swipe gesture
134 */
135 public void enableSwipe() {
136 mRefreshLayout.setEnabled(true);
137 }
138
139 /**
140 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
141 * refreshing programmatically.
142 */
143 public void disableSwipe() {
144 mRefreshLayout.setEnabled(false);
145 }
146
147 /**
148 * It shows the SwipeRefreshLayout progress
149 */
150 public void showSwipeProgress() {
151 mRefreshLayout.setRefreshing(true);
152 }
153
154 /**
155 * It shows the SwipeRefreshLayout progress
156 */
157 public void hideSwipeProgress() {
158 mRefreshLayout.setRefreshing(false);
159 }
160
161
162 }