Tuesday, July 31, 2012

[android-developers] ListFragment not retaining scroll position after orientation change


import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;

public class MyFrag extends ListFragment implements LoaderCallbacks<Cursor> {
private static final String TAG = MyFrag.class.getSimpleName();

private MyAdapter myAdapter;
private int index = 0;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, "onActivityCreated savedInstanceState=" + savedInstanceState);
super.onActivityCreated(savedInstanceState);
myAdapter = new MyAdapter(getActivity(), null, true, getListView());
setListAdapter(myAdapter);

getLoaderManager().initLoader(0, null, this);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated savedInstanceState=" + savedInstanceState);

// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, "onCreate savedInstanceState=" + savedInstanceState);

super.onCreate(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("lv_index", index);
super.onSaveInstanceState(outState);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub

Log.d(TAG, "creating loader");
CursorLoader cl = new CursorLoader(getActivity(), Contacts.CONTENT_URI, new String[] {
Contacts._ID, Contacts.DISPLAY_NAME
}, null, null, null);

return cl;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
// TODO Auto-generated method stub
myAdapter.swapCursor(newCursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
myAdapter.swapCursor(null);
}
}

After struggling with it for a day, this is what i found.

I am using ListFragment and Loaders with support library.

If I used same/standard Loaders code in a normal Activity it ListView maintains its scroll position very precisely. 

If I am using same/standar Loaders code in ListFragment then if I scroll position is complete lost afte orientation change. And it ListView is scrolled to top.

I also noticed that 'savedInstanceState' is coming as 'null' to for 'onCreate', 'onViewCreated' and 'onActivityCreated', even though I am saving some state in 'onSaveInstanceState'.

Attached is the code for fragment.

I am just wondering...am I alone..:-/...?


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate