Ongloballayoutlistener Vs Postrunnable
Solution 1:
If all you are trying to do is read the view's x
and y
coordinates, I recommend using view.post(Runnable)
with no delay (unless there is a good reason to include a delay). This will add the Runnable
to a message queue to in the UI thread. The Runnable
will wait to execute until after your View
is inflated and attached to the window. Since View
position property values depend on the view's layout context, posting a Runnable
will give you the timing that you are looking for.
As you mentioned in your question description, an OnGlobalLayoutListener
will apply to the entire View
's layout as the class name suggests. An OnGlobalLayoutListener
should only be considered if you are concerned with the layout state or visibility of any or all views within the view tree. I.e. anything that causes the view tree to be re-laid out.
Solution 2:
Code reviewer is not happy because you wait 2s and guess that the loading of the activity is finished by then. This may be the case with your emulator or device but on older and slower devices the activity may not have finished loading. To be 100% safe that the activity has finished loading you should use the listener to inform the 'listener' when loading is completed.
Solution 3:
I think that the correct way of doing this is by adding an onPreDrawListener to the view. This is the listener that is called when the view is about to be drawn, and where you already have all the information about it's size (width and height) and position (X and Y).
Example:
finalViewv=newView(getContext());
v.getViewTreeObserver().addOnPreDrawListener(newViewTreeObserver.OnPreDrawListener() {
@OverridepublicbooleanonPreDraw() {
v.getViewTreeObserver().removeOnPreDrawListener(this);
floatmyX= v.getX();
floatmyY= v.getY();
returntrue;
}
});
Don't forget to make the method onPreDraw return true, I don't know why but Android Studio makes it return false when you create the listener. Also don't forget to remove the listener from the view, otherwise it might be uselessly called again.
Post a Comment for "Ongloballayoutlistener Vs Postrunnable"