android ScrollView .scrollTo不工作?保存旋转时的ScrollView位置

tvmytwxo  于 2023-02-06  发布在  Android
关注(0)|答案(7)|浏览(129)

好吧. a.我必须忽略一些真实的简单的东西在这里,但我想我正在尝试做一些相当基本的. a.简单地保留滚动条的位置滚动视图的方向变化...
这是我的onSaveInstanceState和onRestoreInstanceState的代码。sView是滚动视图布局的容器。在我的滚动视图中是一个线性布局,有很多文本视图。

@Override 
public void onSaveInstanceState(Bundle outState) 
{
    //---save whatever you need to persist—

    outState.putInt("sViewX",sView.getScrollX());
    outState.putInt("sViewY",sView.getScrollY());

super.onSaveInstanceState(outState);

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);

    sViewX = savedInstanceState.getInt("sViewX");   
    sViewY = savedInstanceState.getInt("sViewY");

    sView.scrollTo(sViewX, sViewY);

}

如果我在Restore上使用sViewX和sViewY的值设置吐司,则这些值将保持正确。
编辑:我只是尝试做一个sView.scrollTo(0,150);在我的onCreate..中,只是想看看这是否会在150 px下打开Activity,但它没有。我认为我的问题与.scrollTo方法有关。

shstlldc

shstlldc1#

我想通了。
由于我在onCreate中使用setText到TextView,因此调用.scrollTo不起作用。
所以现在我使用以下代码:

sView.post(new Runnable() {
    @Override
    public void run() {
        sView.scrollTo(sViewX, sViewY);
    } 
});
jucafojl

jucafojl2#

在绘制元件之前,不应开始滚动,因为只有在未创建元件时,滚动才起作用:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    scrollView.scrollTo(....
ybzsozfc

ybzsozfc3#

onRestoreInstanceState()只是太早滚动视图。这就是为什么发布新的Runnable会有帮助,但并不总是如此。有时候我们甚至必须使用postDelayed()才能让它工作。对于Fragment,我们可以使用onViewCreated()代替:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    sViewX = savedInstanceState.getInt("sViewX");   
    sViewY = savedInstanceState.getInt("sViewY");
    sView.scrollTo(sViewX, sViewY);
}
bf1o4zei

bf1o4zei4#

这对我很有效

@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));
}
zf2sa74q

zf2sa74q5#

这个代码为我工作的动态滚动标签,也许这将是有用的

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());
  }
});
ippsafx7

ippsafx76#

对于MVVMCross:

protected override void OnSaveInstanceState(Bundle outState)
{
    base.OnSaveInstanceState(outState);

    ScrollView sv = FindViewById<ScrollView>(Resource.Id.dispatchScrollView);
    int posY = sv.ScrollY;

    outState.PutInt("scrollY", posY);
}

protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
    base.OnRestoreInstanceState(savedInstanceState);

    ScrollView sv = FindViewById<ScrollView>(Resource.Id.dispatchScrollView);
    int posY = savedInstanceState.GetInt("scrollY");

    sv.Post(new Runnable(new Action(() => sv.ScrollTo(0, posY))));
}
nafvub8i

nafvub8i7#

您可以在全局布局回调中滚动视图,而不是将滚动操作发送到下一个run-loop:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    sView.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                sView.scrollTo(sViewX, sViewY);
            }
        }
    );
}

相关问题