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