ScrollView .scrollTo not working? Saving ScrollView position on rotation

前端 未结 7 1486
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 02:18

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 orienta

相关标签:
7条回答
  • 2020-12-05 02:22

    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(.... 
    
    0 讨论(0)
  • 2020-12-05 02:24

    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);
    }
    
    0 讨论(0)
  • 2020-12-05 02:24

    For 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))));
    }
    
    0 讨论(0)
  • 2020-12-05 02:27

    Instead of sending scroll action to next run-loop, you can scroll your view in global layout callback:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        sView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    sView.scrollTo(sViewX, sViewY);
                }
            }
        );
    }
    
    0 讨论(0)
  • 2020-12-05 02:32

    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);
        } 
    });
    
    0 讨论(0)
  • 2020-12-05 02:37

    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));
    }
    
    0 讨论(0)
提交回复
热议问题