ExtendedListFragment.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Log_OC;
  21. import com.owncloud.android.R;
  22. import com.owncloud.android.ui.ExtendedListView;
  23. import android.os.Bundle;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.AdapterView;
  28. import android.widget.ListAdapter;
  29. import android.widget.AdapterView.OnItemClickListener;
  30. import android.widget.ListView;
  31. /**
  32. * TODO extending SherlockListFragment instead of SherlockFragment
  33. */
  34. public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener {
  35. private static final String TAG = ExtendedListFragment.class.getSimpleName();
  36. private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
  37. protected ExtendedListView mList;
  38. public void setListAdapter(ListAdapter listAdapter) {
  39. mList.setAdapter(listAdapter);
  40. mList.invalidate();
  41. }
  42. public ListView getListView() {
  43. return mList;
  44. }
  45. @Override
  46. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  47. Log_OC.e(TAG, "onCreateView");
  48. //mList = new ExtendedListView(getActivity());
  49. View v = inflater.inflate(R.layout.list_fragment, null);
  50. mList = (ExtendedListView)(v.findViewById(R.id.list_root));
  51. mList.setOnItemClickListener(this);
  52. //mList.setEmptyView(v.findViewById(R.id.empty_list_view)); // looks like it's not a cool idea
  53. mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
  54. mList.setDividerHeight(1);
  55. if (savedInstanceState != null) {
  56. int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
  57. setReferencePosition(referencePosition);
  58. }
  59. return v;
  60. }
  61. @Override
  62. public void onSaveInstanceState(Bundle savedInstanceState) {
  63. super.onSaveInstanceState(savedInstanceState);
  64. Log_OC.e(TAG, "onSaveInstanceState()");
  65. savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
  66. }
  67. /**
  68. * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
  69. * the device is turned to other position.
  70. *
  71. * THe current policy is take as a reference the visible item in the center of the screen.
  72. *
  73. * @return The position in the list of the visible item in the center of the screen.
  74. */
  75. protected int getReferencePosition() {
  76. if (mList != null) {
  77. return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2;
  78. } else {
  79. return 0;
  80. }
  81. }
  82. /**
  83. * Sets the visible part of the list from the reference position.
  84. *
  85. * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
  86. */
  87. protected void setReferencePosition(int position) {
  88. if (mList != null) {
  89. mList.setAndCenterSelection(position);
  90. }
  91. }
  92. @Override
  93. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  94. // to be @overriden
  95. }
  96. }