Scrollview .scrollto Not Working? Saving Scrollview Position On Rotation
Ok.. I must be overlooking something real simple here, but i think i'm trying to do something fairly basic.. Simply retain the scrollbar position of a ScrollView on orientation cha
Solution 1:
I figured it out.
Since I'm using setText
to TextViews in my onCreate
, calling .scrollTo
won't work.
So now I'm using the following:
sView.post(new Runnable() {
@Override
public void run() {
sView.scrollTo(sViewX, sViewY);
}
});
Solution 2:
onRestoreInstanceState() is just to early to scroll the view. That's why posting new Runnable helps, but not always. Sometimes one even have to use postDelayed() to let it work. For Fragment one can use onViewCreated() instead :
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sViewX = savedInstanceState.getInt("sViewX");
sViewY = savedInstanceState.getInt("sViewY");
sView.scrollTo(sViewX, sViewY);
}
Solution 3:
This is working for me
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SystemGlobal.SCROLL_Y, mRelativeLayoutMain.getTop());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
mRelativeLayoutMain.scrollTo(0, savedInstanceState.getInt(SystemGlobal.SCROLL_Y));
}
Solution 4:
You should start scroll before components are drawn, since scroll does not work untill components are not created:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
scrollView.scrollTo(....
Solution 5:
this code worked for me for dynamically scrolling tabs, maybe it will be useful for u
TabHost tabHost;
int currentActiveTab;
HorizontalScrollView tabsHorizontalScrollView;
//some code ...
tabHost = getTabHost();
tabsHorizontalScrollView = findViewById(R.id.tabsHorizontalScrollView);
currentActiveTab = 8;
//some code ...
tabHost.setCurrentTab(currentActiveTab);
tabHost.getTabWidget().getChildAt(currentActiveTab).post(new Runnable() {
@Override
public void run() {
tabsHorizontalScrollView.scrollTo(tabHost.getTabWidget().getChildAt(currentActiveTab).getLeft(), tabsHorizontalScrollView.getScrollY());
}
});
Post a Comment for "Scrollview .scrollto Not Working? Saving Scrollview Position On Rotation"