ExtendedListFragment.java 5.3 KB

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