1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.fragment
;
21 import android
.os
.Bundle
;
22 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
23 import android
.view
.LayoutInflater
;
24 import android
.view
.View
;
25 import android
.view
.ViewGroup
;
26 import android
.widget
.AdapterView
;
27 import android
.widget
.AdapterView
.OnItemClickListener
;
28 import android
.widget
.ListAdapter
;
29 import android
.widget
.ListView
;
30 import android
.widget
.TextView
;
32 import com
.actionbarsherlock
.app
.SherlockFragment
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.ui
.ExtendedListView
;
35 import com
.owncloud
.android
.utils
.Log_OC
;
38 * TODO extending SherlockListFragment instead of SherlockFragment
40 public class ExtendedListFragment
extends SherlockFragment
implements OnItemClickListener
, SwipeRefreshLayout
.OnRefreshListener
{
42 private static final String TAG
= ExtendedListFragment
.class.getSimpleName();
44 private static final String KEY_SAVED_LIST_POSITION
= "SAVED_LIST_POSITION";
46 protected ExtendedListView mList
;
48 private SwipeRefreshLayout mRefreshLayout
;
49 private SwipeRefreshLayout mRefreshEmptyLayout
;
50 private TextView mEmptyListMessage
;
52 public void setListAdapter(ListAdapter listAdapter
) {
53 mList
.setAdapter(listAdapter
);
57 public ListView
getListView() {
63 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
64 Log_OC
.e(TAG
, "onCreateView");
65 //mList = new ExtendedListView(getActivity());
67 View v
= inflater
.inflate(R
.layout
.list_fragment
, null
);
68 mEmptyListMessage
= (TextView
) v
.findViewById(R
.id
.empty_list_view
);
69 mList
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
));
70 mList
.setOnItemClickListener(this);
72 mList
.setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
));
73 mList
.setDividerHeight(1);
75 if (savedInstanceState
!= null
) {
76 int referencePosition
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
);
77 setReferencePosition(referencePosition
);
81 mRefreshLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
);
82 mRefreshEmptyLayout
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
);
84 onCreateSwipeToRefresh(mRefreshLayout
);
85 onCreateSwipeToRefresh(mRefreshEmptyLayout
);
87 mList
.setEmptyView(mRefreshEmptyLayout
);
94 public void onSaveInstanceState(Bundle savedInstanceState
) {
95 super.onSaveInstanceState(savedInstanceState
);
96 Log_OC
.e(TAG
, "onSaveInstanceState()");
97 savedInstanceState
.putInt(KEY_SAVED_LIST_POSITION
, getReferencePosition());
102 * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
103 * the device is turned to other position.
105 * THe current policy is take as a reference the visible item in the center of the screen.
107 * @return The position in the list of the visible item in the center of the screen.
109 protected int getReferencePosition() {
111 return (mList
.getFirstVisiblePosition() + mList
.getLastVisiblePosition()) / 2;
119 * Sets the visible part of the list from the reference position.
121 * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
123 protected void setReferencePosition(int position
) {
125 mList
.setAndCenterSelection(position
);
130 public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) {
135 public void onRefresh() {
137 mRefreshLayout
.setRefreshing(false
);
138 mRefreshEmptyLayout
.setRefreshing(false
);
142 * Enables swipe gesture
144 public void enableSwipe() {
145 mRefreshLayout
.setEnabled(true
);
149 * Disables swipe gesture. It prevents manual gestures but keeps the option you show
150 * refreshing programmatically.
152 public void disableSwipe() {
153 mRefreshLayout
.setEnabled(false
);
157 * It shows the SwipeRefreshLayout progress
159 public void showSwipeProgress() {
160 mRefreshLayout
.setRefreshing(true
);
164 * It shows the SwipeRefreshLayout progress
166 public void hideSwipeProgress() {
167 mRefreshLayout
.setRefreshing(false
);
171 * Set message for empty list view
173 public void setMessageForEmptyList(String message
) {
174 if (mEmptyListMessage
!= null
) {
175 mEmptyListMessage
.setText(message
);
180 * Get the text of EmptyListMessage TextView
184 public String
getEmptyViewText() {
185 return (mEmptyListMessage
!= null
) ? mEmptyListMessage
.getText().toString() : "";
188 private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout
) {
189 // Colors in animations: background
190 refreshLayout
.setColorScheme(R
.color
.background_color
, R
.color
.background_color
, R
.color
.background_color
,
191 R
.color
.background_color
);
193 refreshLayout
.setOnRefreshListener(this);