2013-10-07 4 views
7

von den offiziellen Dokumenten:Warum view.onLayout() wiederholt aufgerufen wird, wenn geändert = false?

Android Docs - View

protected void onLayout (boolean changed, int left, int top, int right, int bottom) 

Parameters 
changed This is a new size or position for this view 
left Left position, relative to parent 
top  Top position, relative to parent 
right Right position, relative to parent 
bottom Bottom position, relative to parent 

Warum ist meine benutzerdefinierte Ansicht wiederholt mit changed=false genannt? Es ist normal?

Hier ist der Aufrufbaum:

Synoptic.onLayout(boolean, int, int, int, int) line: 130  
Synoptic(View).layout(int, int, int, int) line: 11282 
Synoptic(ViewGroup).layout(int, int, int, int) line: 4224 
RelativeLayout.onLayout(boolean, int, int, int, int) line: 925 
RelativeLayout(View).layout(int, int, int, int) line: 11282 
RelativeLayout(ViewGroup).layout(int, int, int, int) line: 4224 
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutHorizontal() line: 1617 
LinearLayout.onLayout(boolean, int, int, int, int) line: 1401 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
SwipeView(FrameLayout).onLayout(boolean, int, int, int, int) line: 431 
SwipeView(HorizontalScrollView).onLayout(boolean, int, int, int, int) line: 1382  
SwipeView.onLayout(boolean, int, int, int, int) line: 154 
SwipeView(View).layout(int, int, int, int) line: 11282 
SwipeView(ViewGroup).layout(int, int, int, int) line: 4224 
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutVertical() line: 1486  
LinearLayout.onLayout(boolean, int, int, int, int) line: 1399 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
FrameLayout.onLayout(boolean, int, int, int, int) line: 431 
FrameLayout(View).layout(int, int, int, int) line: 11282  
FrameLayout(ViewGroup).layout(int, int, int, int) line: 4224  
LinearLayout.setChildFrame(View, int, int, int, int) line: 1628 
LinearLayout.layoutVertical() line: 1486  
LinearLayout.onLayout(boolean, int, int, int, int) line: 1399 
LinearLayout(View).layout(int, int, int, int) line: 11282 
LinearLayout(ViewGroup).layout(int, int, int, int) line: 4224 
PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 431 
PhoneWindow$DecorView(View).layout(int, int, int, int) line: 11282 
PhoneWindow$DecorView(ViewGroup).layout(int, int, int, int) line: 4224 
ViewRootImpl.performTraversals() line: 1514 
ViewRootImpl.handleMessage(Message) line: 2467 
ViewRootImpl(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4424  
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
Method.invoke(Object, Object...) line: 511 
ZygoteInit$MethodAndArgsCaller.run() line: 784 
ZygoteInit.main(String[]) line: 551 
NativeStart.main(String[]) line: not available [native method] 
+0

jemand anfordert Layout? –

+0

Der Rest meines Intarface scheint statisch zu sein (keine Animationen), aber es gibt viele relative Layouts. –

Antwort

6

ich das Problem gefunden: für eine Verbindung Komponente wie meine, dass eine TextView enthält, verursacht die wiederholte Aufruf .setText() einen Aufruf an die onLayout()

der Eltern ... auch wenn der neue Text gleich dem Strom ist TextView Text!

so gelöst I:

public void setTextViewText(String text) { 
    if (!this.textViewValue.getText().equals(text)) { 
     this.textViewValue.setText(text); 
    } 
} 
10

Der wichtigste Teil hier von der Dokumentation ist:

von Layout aufgerufen, wenn diese Ansicht sollte eine Größe und Position jedes seiner Kinder zuweisen.

Wenn eine View in einer ViewGroup auf wrap_content gesetzt ist und ihre Größe ändert (z. B. ändert sich der Text in einer TextView in eine längere Zeichenfolge), wird ein onLayout in der ViewGroup angezeigt. Dies liegt daran, dass Ansichten ihre Größe erhalten, wenn sie angelegt werden. Ohne Aufruf erhält die Ansicht daher nie eine neue Größe.

Verwandte Themen