安卓:通知滚动型,它的孩子的尺寸发生了变化:怎么了? [英] Android: Notify Scrollview that it's child's size has changed: how?

查看:177
本文介绍了安卓:通知滚动型,它的孩子的尺寸发生了变化:怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我放大滚动视图的内容的大小,滚动视图需要一段时间去知道它的这种规模变化的孩子。如何订购的滚动型以检查它的孩子立刻?

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 check it's child immediately?

我有一个滚动型中的LinearLayout的ImageView的。

I have an ImageView in a LinearLayout in a ScrollView.

在我ScaleListener.onScale,我改变我的LinearLayout的大小。然后我试着订单上的滚动视图滚动。在ScaleListener.onScale:

In my ScaleListener.onScale, I change the size of my LinearLayout. I then try to order a scroll on the scrollview. In the ScaleListener.onScale:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
params.width = (int) (startX * scaleFactor);
params.height = (int) (startY * scaleFactor);
imageView.setLayoutParams(params);

(...)

scrollView.scrollBy(scrollX, scrollY);

然而,没有发生滚动时的情况下缩放滚动之前,是不可能的,因为认为太小滚动。在setLayoutParams后,认为要大一些,但不会发生滚动,因为滚动视图认为孩子还小。

However, no scrolling occurs when in the situation before the scaling scrolling was not possible because the view was too small to scroll. After the setLayoutParams, the view should be larger, but no scrolling occurs because the scrollview thinks the child is still small.

在FES毫秒后的onScroll被再次调用,它滚动很好,它在某种程度上发现,孩子是大,滚动的。

When a fes ms later the onScroll is called again, it does scroll fine, it somehow found out that the child is larger and scrollable.

我怎样才能立即通知滚动视图,孩子的大小发生了变化?这样scrollBy将它的孩子setLayoutParams之后的工作?

How can I notify the scrollview immediately, that the child's size has changed? So that scrollBy will work right after setLayoutParams on it's child?

推荐答案

我试图发现几乎每一个 onXXX()法后的解决方案。 onLayout都可以使用。你可以计划滚动,后来做它在 onLayout()

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;
    }
}

现在,用它代替这在code,scrollBy(X,Y)使用 planScrollBy(X,Y)。它会做滚动的时候是已知的孩子的新大小,但在屏幕上没有显示任何时间。

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.

当您使用水平或垂直滚动​​型,当然你也可以只滚动一条路,所以你必须要改变这个code有点(或没有,但它会忽略另一个轴滚动) 。我用了一个 TwoDScrollView ,你可以找到它在网络上。

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.

这篇关于安卓:通知滚动型,它的孩子的尺寸发生了变化:怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆