ExtendedListFragment.java 10 KB

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