ExtendedListFragment.java 6.2 KB

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