Android: Notify Scrollview that it's child's size has changed: how?

前端 未结 2 1556
梦谈多话
梦谈多话 2020-12-10 19:57

When I enlarge the size of the content of a scrollview, the scrollview takes a while to get to \"know\" this size change of it\'s child. How can I order the ScrollView to ch

相关标签:
2条回答
  • 2020-12-10 20:32

    You can call:

    scrollView.updateViewLayout(childView, childLayout)
    
    0 讨论(0)
  • 2020-12-10 20:42

    I found a solution after trying just about every onXXX() method. onLayout can be used. You can plan the scroll and do it later in onLayout().

    Extend your scrollview, and add:

    private int onLayoutScrollByX = 0;
    private int onLayoutScrollByY = 0;
    
    public void planScrollBy(int x, int y) {
        onLayoutScrollByX += x;
        onLayoutScrollByY += y;
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        doPlannedScroll();
    }
    
    public void doPlannedScroll() {
        if (onLayoutScrollByX != 0 || onLayoutScrollByY != 0) {
            scrollBy(onLayoutScrollByX, onLayoutScrollByY);
            onLayoutScrollByX = 0;
            onLayoutScrollByY = 0;
        }
    }
    

    Now, to use this in your code, instead of scrollBy(x,y) use planScrollBy(x,y). It will do the scroll at a time when the new size of the child is "known", but not displayed on screen yet.

    When you use a horizontal or vertical scrollview, of course you can only scroll one way, so you will have to change this code it a bit (or not, but it will ignore the scroll on the other axis). I used a TwoDScrollView, you can find it on the web.

    0 讨论(0)
提交回复
热议问题