ExtendedListFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. package com.owncloud.android.ui.fragment;
  19. import java.util.ArrayList;
  20. import android.os.Bundle;
  21. import android.support.v4.widget.SwipeRefreshLayout;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.AdapterView;
  26. import android.widget.AdapterView.OnItemClickListener;
  27. import android.widget.GridView;
  28. import android.widget.ListAdapter;
  29. import android.widget.TextView;
  30. import com.actionbarsherlock.app.SherlockFragment;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import third_parties.in.srain.cube.GridViewWithHeaderAndFooter;
  34. import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
  35. /**
  36. * TODO extending SherlockListFragment instead of SherlockFragment
  37. */
  38. public class ExtendedListFragment extends SherlockFragment
  39. implements OnItemClickListener, OnEnforceableRefreshListener {
  40. private static final String TAG = ExtendedListFragment.class.getSimpleName();
  41. private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
  42. private static final String KEY_INDEXES = "INDEXES";
  43. private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
  44. private static final String KEY_TOPS = "TOPS";
  45. private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
  46. private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
  47. private SwipeRefreshLayout mRefreshLayout;
  48. private SwipeRefreshLayout mRefreshEmptyLayout;
  49. private TextView mEmptyListMessage;
  50. // Save the state of the scroll in browsing
  51. private ArrayList<Integer> mIndexes;
  52. private ArrayList<Integer> mFirstPositions;
  53. private ArrayList<Integer> mTops;
  54. private int mHeightCell = 0;
  55. private OnEnforceableRefreshListener mOnRefreshListener = null;
  56. protected GridViewWithHeaderAndFooter imageView;
  57. public void setListAdapter(ListAdapter listAdapter) {
  58. imageView.setAdapter(listAdapter);
  59. imageView.invalidate();
  60. }
  61. public GridView getGridView() {
  62. return imageView;
  63. }
  64. public void setFooterView(View footer) {
  65. imageView.addFooterView(footer, null, false);
  66. imageView.invalidate();
  67. }
  68. public void removeFooterView(View footer) {
  69. imageView.removeFooterView(footer);
  70. imageView.invalidate();
  71. }
  72. public int getFooterViewCount() {
  73. return imageView.getFooterViewCount();
  74. }
  75. protected void switchImageView(){
  76. imageView.setNumColumns(GridView.AUTO_FIT);
  77. imageView.invalidateRowHeight(); // Force to recalculate mRowHeight of imageView
  78. imageView.invalidate();
  79. }
  80. protected void switchFileView(){
  81. imageView.setNumColumns(1);
  82. imageView.invalidate();
  83. }
  84. @Override
  85. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  86. Log_OC.e(TAG, "onCreateView");
  87. View v = inflater.inflate(R.layout.list_fragment, null);
  88. imageView = (GridViewWithHeaderAndFooter)(v.findViewById(R.id.list_root));
  89. imageView.setOnItemClickListener(this);
  90. if (savedInstanceState != null) {
  91. int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
  92. setReferencePosition(referencePosition);
  93. }
  94. // Pull down refresh
  95. mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files);
  96. mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_files_emptyView);
  97. mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
  98. onCreateSwipeToRefresh(mRefreshLayout);
  99. onCreateSwipeToRefresh(mRefreshEmptyLayout);
  100. imageView.setEmptyView(mRefreshEmptyLayout);
  101. return v;
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. @Override
  107. public void onActivityCreated(Bundle savedInstanceState) {
  108. super.onActivityCreated(savedInstanceState);
  109. if (savedInstanceState != null) {
  110. mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
  111. mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
  112. mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
  113. mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
  114. setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
  115. } else {
  116. mIndexes = new ArrayList<Integer>();
  117. mFirstPositions = new ArrayList<Integer>();
  118. mTops = new ArrayList<Integer>();
  119. mHeightCell = 0;
  120. }
  121. }
  122. @Override
  123. public void onSaveInstanceState(Bundle savedInstanceState) {
  124. super.onSaveInstanceState(savedInstanceState);
  125. Log_OC.e(TAG, "onSaveInstanceState()");
  126. savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
  127. savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
  128. savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
  129. savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
  130. savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
  131. savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
  132. }
  133. /**
  134. * Calculates the position of the item that will be used as a reference to
  135. * reposition the visible items in the list when the device is turned to
  136. * other position.
  137. *
  138. * THe current policy is take as a reference the visible item in the center
  139. * of the screen.
  140. *
  141. * @return The position in the list of the visible item in the center of the
  142. * screen.
  143. */
  144. protected int getReferencePosition() {
  145. if (imageView != null) {
  146. return (imageView.getFirstVisiblePosition() + imageView.getLastVisiblePosition()) / 2;
  147. } else {
  148. return 0;
  149. }
  150. }
  151. /**
  152. * Sets the visible part of the list from the reference position.
  153. *
  154. * @param position Reference position previously returned by
  155. * {@link LocalFileListFragment#getReferencePosition()}
  156. */
  157. protected void setReferencePosition(int position) {
  158. if (imageView != null) {
  159. imageView.setSelection(position);
  160. }
  161. }
  162. /*
  163. * Restore index and position
  164. */
  165. protected void restoreIndexAndTopPosition() {
  166. if (mIndexes.size() > 0) {
  167. // needs to be checked; not every browse-up had a browse-down before
  168. int index = mIndexes.remove(mIndexes.size() - 1);
  169. int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
  170. int top = mTops.remove(mTops.size() - 1);
  171. imageView.smoothScrollToPosition(firstPosition);
  172. // Move the scroll if the selection is not visible
  173. int indexPosition = mHeightCell*index;
  174. int height = imageView.getHeight();
  175. if (indexPosition > height) {
  176. imageView.smoothScrollToPosition(index);
  177. }
  178. }
  179. }
  180. /*
  181. * Save index and top position
  182. */
  183. protected void saveIndexAndTopPosition(int index) {
  184. mIndexes.add(index);
  185. int firstPosition = imageView.getFirstVisiblePosition();
  186. mFirstPositions.add(firstPosition);
  187. View view = imageView.getChildAt(0);
  188. int top = (view == null) ? 0 : view.getTop() ;
  189. mTops.add(top);
  190. // Save the height of a cell
  191. mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
  192. }
  193. @Override
  194. public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
  195. // to be @overriden
  196. }
  197. @Override
  198. public void onRefresh() {
  199. // to be @overriden
  200. mRefreshLayout.setRefreshing(false);
  201. mRefreshEmptyLayout.setRefreshing(false);
  202. if (mOnRefreshListener != null) {
  203. mOnRefreshListener.onRefresh();
  204. }
  205. }
  206. public void setOnRefreshListener(OnEnforceableRefreshListener listener) {
  207. mOnRefreshListener = listener;
  208. }
  209. /**
  210. * Enables swipe gesture
  211. */
  212. public void enableSwipe() {
  213. mRefreshLayout.setEnabled(true);
  214. }
  215. /**
  216. * Disables swipe gesture. It prevents manual gestures but keeps the option you show
  217. * refreshing programmatically.
  218. */
  219. public void disableSwipe() {
  220. mRefreshLayout.setEnabled(false);
  221. }
  222. /**
  223. * It shows the SwipeRefreshLayout progress
  224. */
  225. public void showSwipeProgress() {
  226. mRefreshLayout.setRefreshing(true);
  227. }
  228. /**
  229. * It shows the SwipeRefreshLayout progress
  230. */
  231. public void hideSwipeProgress() {
  232. mRefreshLayout.setRefreshing(false);
  233. }
  234. /**
  235. * Set message for empty list view
  236. */
  237. public void setMessageForEmptyList(String message) {
  238. if (mEmptyListMessage != null) {
  239. mEmptyListMessage.setText(message);
  240. }
  241. }
  242. /**
  243. * Get the text of EmptyListMessage TextView
  244. *
  245. * @return String
  246. */
  247. public String getEmptyViewText() {
  248. return (mEmptyListMessage != null) ? mEmptyListMessage.getText().toString() : "";
  249. }
  250. private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout) {
  251. // Colors in animations: background
  252. refreshLayout.setColorScheme(R.color.background_color, R.color.background_color, R.color.background_color,
  253. R.color.background_color);
  254. refreshLayout.setOnRefreshListener(this);
  255. }
  256. @Override
  257. public void onRefresh(boolean ignoreETag) {
  258. mRefreshLayout.setRefreshing(false);
  259. mRefreshEmptyLayout.setRefreshing(false);
  260. if (mOnRefreshListener != null) {
  261. mOnRefreshListener.onRefresh(ignoreETag);
  262. }
  263. }
  264. }