Fix: animations should be gone. Color default animations is background_color
[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 // Colors in animations: background
80 mRefreshLayout.setColorScheme(R.color.background_color, R.color.background_color,
81 R.color.background_color, R.color.background_color);
82
83 mRefreshLayout.setOnRefreshListener(this);
84
85 return v;
86 }
87
88
89 @Override
90 public void onSaveInstanceState(Bundle savedInstanceState) {
91 super.onSaveInstanceState(savedInstanceState);
92 Log_OC.e(TAG, "onSaveInstanceState()");
93 savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
94 }
95
96
97 /**
98 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
99 * the device is turned to other position.
100 *
101 * THe current policy is take as a reference the visible item in the center of the screen.
102 *
103 * @return The position in the list of the visible item in the center of the screen.
104 */
105 protected int getReferencePosition() {
106 if (mList != null) {
107 return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
108 } else {
109 return 0;
110 }
111 }
112
113
114 /**
115 * Sets the visible part of the list from the reference position.
116 *
117 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
118 */
119 protected void setReferencePosition(int position) {
120 if (mList != null) {
121 mList.setAndCenterSelection(position);
122 }
123 }
124
125 @Override
126 public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
127 // to be @overriden
128 }
129
130 @Override
131 public void onRefresh() {
132 // to be @overriden
133 mRefreshLayout.setRefreshing(false);
134 }
135
136 /**
137 * Enables swipe gesture
138 */
139 public void enableSwipe() {
140 mRefreshLayout.setEnabled(true);
141 }
142
143 /**
144 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
145 * refreshing programmatically.
146 */
147 public void disableSwipe() {
148 mRefreshLayout.setEnabled(false);
149 }
150
151 /**
152 * It shows the SwipeRefreshLayout progress
153 */
154 public void showSwipeProgress() {
155 mRefreshLayout.setRefreshing(true);
156 }
157
158 /**
159 * It shows the SwipeRefreshLayout progress
160 */
161 public void hideSwipeProgress() {
162 mRefreshLayout.setRefreshing(false);
163 }
164
165
166 }