--- /dev/null
+package com.owncloud.android.ui;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.util.AttributeSet;
+import android.widget.ListView;
+
+/**
+ * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
+ *
+ * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
+ *
+ * @author David A. Velasco
+ */
+public class ExtendedListView extends ListView {
+
+ private int mPositionToSetAndCenter;
+
+ public ExtendedListView(Context context) {
+ super(context);
+ }
+
+ public ExtendedListView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public ExtendedListView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ *
+ */
+ @Override
+ protected void onDraw (Canvas canvas) {
+ super.onDraw(canvas);
+ if (mPositionToSetAndCenter > 0) {
+ this.setSelectionFromTop(mPositionToSetAndCenter, getHeight() / 2);
+ mPositionToSetAndCenter = 0;
+ }
+ }
+
+ /**
+ * Public method to set the position of the item that should be centered in the visible area of the view.
+ *
+ * The position is saved here and checked in onDraw().
+ *
+ * @param position Position (in the list of items) of the item to center in the visible area.
+ */
+ public void setAndCenterSelection(int position) {
+ mPositionToSetAndCenter = position;
+ }
+}
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListAdapter;
-import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ListView;
public class FragmentListView extends SherlockFragment implements
OnItemClickListener, OnItemLongClickListener {
- protected ListView mList;
+ protected ExtendedListView mList;
@Override
public void onCreate(Bundle savedInstanceState) {
public ListView getListView() {
return mList;
}
-
+
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
- mList = new ListView(getActivity());
+ mList = new ExtendedListView(getActivity());
mList.setOnItemClickListener(this);
mList.setOnItemLongClickListener(this);
return mList;
- // return super.onCreateView(inflater, container, savedInstanceState);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
\r
// List current directory\r
- mFileList.listDirectory(mCurrentDir); // we should find the way to avoid the need of this\r
+ mFileList.listDirectory(mCurrentDir); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check)\r
\r
} else {\r
\r
mStorageManager.saveFile(newDir);\r
\r
// Display the new folder right away\r
- mFileList.listDirectory(mCurrentDir);\r
+ mFileList.listDirectory();\r
}\r
});\r
\r
OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager()\r
.findFragmentById(R.id.fileList);\r
if (fileListFragment != null) {\r
- fileListFragment.listDirectory(mCurrentDir); \r
+ fileListFragment.listDirectory(mCurrentDir);\r
}\r
}\r
\r
import java.io.File;
+import com.owncloud.android.ui.ExtendedListView;
import com.owncloud.android.ui.FragmentListView;
import com.owncloud.android.ui.adapter.LocalFileListAdapter;
*/
public class LocalFileListFragment extends FragmentListView {
private static final String TAG = "LocalFileListFragment";
+ private static final String SAVED_LIST_POSITION = "LIST_POSITION";
/** Reference to the Activity which this fragment is attached to. For callbacks */
private LocalFileListFragment.ContainerActivity mContainerActivity;
if (savedInstanceState != null) {
Log.i(TAG, "savedInstanceState is not null");
- int position = savedInstanceState.getInt("LIST_POSITION");
- getListView().setSelectionFromTop(position, 0);
+ int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
+ setReferencePosition(position);
}
Log.i(TAG, "onActivityCreated() stop");
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onSaveInstanceState() start");
- savedInstanceState.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
+ savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
+
Log.i(TAG, "onSaveInstanceState() stop");
}
/**
+ * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
+ * the device is turned to other position.
+ *
+ * THe current policy is take as a reference the visible item in the center of the screen.
+ *
+ * @return The position in the list of the visible item in the center of the screen.
+ */
+ private int getReferencePosition() {
+ return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
+ }
+
+
+ /**
+ * Sets the visible part of the list from the reference position.
+ *
+ * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()}
+ */
+ private void setReferencePosition(int position) {
+ ((ExtendedListView)getListView()).setAndCenterSelection(position);
+ }
+
+
+ /**
* Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
*/
@Override
directory = directory.getParentFile();
}
- mDirectory = directory;
mList.clearChoices(); // by now, only files in the same directory will be kept as selected
- mAdapter.swapDirectory(mDirectory);
- mList.setSelectionFromTop(0, 0);
+ mAdapter.swapDirectory(directory);
+ if (mDirectory == null || !mDirectory.equals(directory)) {
+ mList.setSelectionFromTop(0, 0);
+ }
+ mDirectory = directory;
}
import com.owncloud.android.datamodel.DataStorageManager;
import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.ui.ExtendedListView;
import com.owncloud.android.ui.FragmentListView;
import com.owncloud.android.ui.activity.TransferServiceGetter;
import com.owncloud.android.ui.adapter.FileListListAdapter;
*/
public class OCFileListFragment extends FragmentListView {
private static final String TAG = "FileListFragment";
+ private static final String SAVED_LIST_POSITION = "LIST_POSITION";
private OCFileListFragment.ContainerActivity mContainerActivity;
if (savedInstanceState != null) {
Log.i(TAG, "savedInstanceState is not null");
- int position = savedInstanceState.getInt("LIST_POSITION");
- getListView().setSelectionFromTop(position, 0);
+ int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
+ setReferencePosition(position);
}
- //mAdapter = new FileListListAdapter();
Log.i(TAG, "onActivityCreated() stop");
}
+
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onSaveInstanceState() start");
- savedInstanceState.putInt("LIST_POSITION", getListView().getFirstVisiblePosition());
+ savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
Log.i(TAG, "onSaveInstanceState() stop");
}
+ /**
+ * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when
+ * the device is turned to other position.
+ *
+ * THe current policy is take as a reference the visible item in the center of the screen.
+ *
+ * @return The position in the list of the visible item in the center of the screen.
+ */
+ private int getReferencePosition() {
+ return (getListView().getFirstVisiblePosition() + getListView().getLastVisiblePosition()) / 2;
+ }
+
+
+ /**
+ * Sets the visible part of the list from the reference position.
+ *
+ * @param position Reference position previously returned by {@link OCFileListFragment#getReferencePosition()}
+ */
+ private void setReferencePosition(int position) {
+ ((ExtendedListView)getListView()).setAndCenterSelection(position);
+ }
+
+
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
OCFile file = (OCFile) mAdapter.getItem(position);
* Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
*/
public void listDirectory(){
- int position = mList.getFirstVisiblePosition();
listDirectory(null);
- mList.setSelectionFromTop(position, 0);
}
/**
directory = storageManager.getFileById(directory.getParentId());
}
- mFile = directory;
mAdapter.swapDirectory(mFile, storageManager);
- mList.setSelectionFromTop(0, 0);
- mList.invalidate();
+ if (mFile == null || !mFile.equals(directory)) {
+ mList.setSelectionFromTop(0, 0);
+ }
+ mFile = directory;
}
}