简体   繁体   English

从新的Runnable()获取价值

[英]Get value from new Runnable()

How can I get the value accessed inside a Runnable ? 我如何获得在Runnable内部访问的值?
I'm trying to get the height from a LinearLayout and I able to see it with Log.d() but I'm just can't store the value in a member var. 我正在尝试从LinearLayout获取高度,并且可以通过Log.d()看到它的高度,但是我只是无法将值存储在成员var中。

This is the code where I see the height: 这是我看到高度的代码:

final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);

scrollView.post(new Runnable() {
    @Override
    public void run() {
        setHeight(scrollView.getHeight());
        Log.d("height", scrollView.getHeight());
    }
});

Then I've tried to store it with a function but it always returns 0 : 然后我尝试用一​​个函数存储它,但它总是返回0

protected void setHeight(int value){
    height = value;
}

How can I store the height value in the height var? 如何将高度值存储在height var中?

this is a classic case of inner class. 这是内部阶级的经典案例。 Basically indirectly we are trying to store a value inside a non final variable height. 基本上间接地,我们试图将值存储在非最终变量高度内。 Hence it could not store the variable. 因此,它无法存储变量。 This is the route cause why you cant store the value of height.You should use other ways from setting the values inside the inner class. 这就是为什么不能存储height值的原因。您应该使用其他方法来设置内部类中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM