【问题标题】:Method returning default/initial value in android在android中返回默认/初始值的方法
【发布时间】:2015-09-27 06:07:16
【问题描述】:

您好,我正在根据我的图像视图获取坐标值。所以,现在我正在尝试全局使用这些坐标值,但我的方法返回零(默认/初始)值,但它在方法内部打印正确的值。请帮帮我..

代码: MainActivity.java

public class StackActivity extends ActionBarActivity {

ImageView firstDot, middleDot, lastDot;

int distaceHoriz;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stack);

    firstDot = (ImageView) findViewById(R.id.img_dot_starting);
    middleDot = (ImageView) findViewById(R.id.img_dot_middle);
    lastDot = (ImageView) findViewById(R.id.img_dot_last);

    distance(firstDot);
    Log.e("distaceHorizFirst", distaceHoriz+"");

    distance(middleDot);
    Log.e("distaceHorizMiddle", distaceHoriz+"");

    distance(lastDot);
    Log.e("distaceHorizLast", distaceHoriz+"");


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.stack, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

 public int distance(final View view) {


    view.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {


                @Override
                public void onGlobalLayout() {

                    view.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);

                    int[] locations = new int[2];
                    view.getLocationOnScreen(locations);
                    distaceHoriz = locations[0];
                    // int vertDistanceToLastDot = locations[1];

                    Log.e("distaceHoriz:", ""+ distaceHoriz);

                }
            });
    return distaceHoriz;

}
}

MainLayout.xml

<ImageView
    android:id="@+id/img_dot_starting"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="42dp"
    android:layout_alignParentLeft="true"
    android:src="@drawable/icon_round" />

<ImageView
    android:id="@+id/img_dot_middle"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="42dp"
    android:layout_centerHorizontal="true"
    android:src="@drawable/icon_round" />

<ImageView
    android:id="@+id/img_dot_last"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="42dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/icon_round" />

【问题讨论】:

  • 嗯,这是因为 OnGlobalLayoutListener 的异步特性。 onGlobalLayout() 的内容不会立即执行,而是在不久的将来某个时间点执行。但是,您的 distance() 方法确实同步运行并立即返回默认值(因为实际分配您所追求的值的代码尚未执行)。我认为您错误地假设通过将侦听器放在您自己的方法中,它的回调会立即运行。情况不是

标签: java android methods return-value


【解决方案1】:

尝试使用addonPreDrawListener

public int distance(final View view) {
int distaceHoriz; 
    ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                view.getViewTreeObserver().removeOnPreDrawListener(this);
               int[] locations = new int[2];
                    view.getLocationOnScreen(locations);
                    distaceHoriz = locations[0];
                    // int vertDistanceToLastDot = locations[1];

                    Log.e("distaceHoriz:", ""+ distaceHoriz);
            }
        });
return distaceHoriz;
}

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多