I have a fragment inside a scroll view.
When the fragment is loaded, the scroll view scrolls down to the fragment.
I want to load the fragment into the scroll view, but I don't want the scroll view to scroll down to the fragment from it's current position(top), when it is loaded.
This is how I load the fragment :
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(bikeFragContainer.getId(),bikeFrag);
ft.show(bikeFrag);
ft.commit();
setBikeFragData();
The reason for the scroll seems to me, is that, ft.commit()
loads the fragment and moves the screen to display the fragment.
Is there any way to load the fragment into the scroll view, without moving the scroll view to the fragment, when it is loaded into the scroll view?
I think the answer lies in finding a way to tell the FragmentManager
or FragmentTransaction
to, just load the fragment but not do any transition. Any suggestion guys ??
Despite your help, I couldn't find a clean solution for my issue.
The reason is that, my approach might not be the right (to include a fragment in a scrollview and expect it not to scroll) and there might be better approaches.
What I did is to fix my issue is, I forced the scroll view, to scroll to the top, right after the fragment is loaded. So the user sees the scroll view scrolling down and up once very fast.(I know clumsy!)
Here is my code :
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(mGridContainer.getId(),bikeFrag);
ft.commit();
setBikeFrag();
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, 0); // scrolls to top of scrollview
}
}, 1000);
I wait one second after fragment transaction takes place and scroll to the top.
May be this delay can be reduced , but I kept it as one second, just to be safe.
scrollView
is the object of the ScrollView widget, in my layout.
It is inside this scroll view, the fragment, along with some other widgets are located.