I have a list fragment.
When the user click on a list item, i replace the list fragment with a news fragment.
When the user come back (using addToBackStack
), the list scrolls to the top.
I would like my list to remain to the same scroll level when the user pressed the list item..is that possible?how can i get this result?
this is my code where i replace the list fragment:
myFragment myFragment = new myFragment();
myFragment.setArguments(bundle);
FragmentTransaction ft = getActivity().getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.phone_container, myFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
I have tried something like this but i get no result:
public void onResume() {
super.onResume();
Log.i("log", "onResume()");
setListAdapter(adapter);
if(index!=-1){
this.getListView().setSelectionFromTop(index, top);
}
}
public void onPause() {
super.onPause();
Log.i("log", "onPause()");
try {
index = this.getListView().getFirstVisiblePosition();
View v = this.getListView().getChildAt(0);
top = (v == null) ? 0 : v.getTop();
} catch (Throwable t) {
t.printStackTrace();
}
}