Android: Improve the way we update layout params for native views.

Removing and adding a view each time the layout parameters changes is
triggering unnecessary layout updates. The affect of this is not very
visible since there are few views in the layout, but the procedure is
never the less wasteful.

Change-Id: I2540ab519b12c6d3e10457e2e518b439118b966d
Reviewed-by: BogDan Vatra <bogdan@kdab.com>
bb10
Christian Strømme 2015-05-29 17:23:37 +02:00 committed by Christian Stromme
parent 5afc431323
commit 1811e31cdb
2 changed files with 33 additions and 6 deletions

View File

@ -312,8 +312,7 @@ public class QtActivityDelegate
m_editText.setImeOptions(imeOptions);
m_editText.setInputType(inputType);
m_layout.removeView(m_editText);
m_layout.addView(m_editText, new QtLayout.LayoutParams(width, height, x, y));
m_layout.setLayoutParams(m_editText, new QtLayout.LayoutParams(width, height, x, y), false);
m_editText.requestFocus();
m_editText.postDelayed(new Runnable() {
@Override
@ -1091,12 +1090,10 @@ public class QtActivityDelegate
if (Build.VERSION.SDK_INT < 11 || w <= 0 || h <= 0) {
m_activity.openContextMenu(m_layout);
} else if (Build.VERSION.SDK_INT < 14) {
m_layout.removeView(m_editText);
m_layout.addView(m_editText, new QtLayout.LayoutParams(w, h, x, y));
m_layout.setLayoutParams(m_editText, new QtLayout.LayoutParams(w, h, x, y), false);
QtPopupMenu.getInstance().showMenu(m_editText);
} else {
m_layout.removeView(m_editText);
m_layout.addView(m_editText, new QtLayout.LayoutParams(w, h, x, y));
m_layout.setLayoutParams(m_editText, new QtLayout.LayoutParams(w, h, x, y), false);
QtPopupMenu14.getInstance().showMenu(m_editText);
}
}

View File

@ -216,4 +216,34 @@ public class QtLayout extends ViewGroup
invalidate();
attachViewToParent(view, index, view.getLayoutParams());
}
/**
* set the layout params on a child view.
*
* Note: This function adds the child view if it's not in the
* layout already.
*/
public void setLayoutParams(final View childView,
final ViewGroup.LayoutParams params,
final boolean forceRedraw)
{
// Invalid view
if (childView == null)
return;
// Invalid params
if (!checkLayoutParams(params))
return;
// View is already in the layout and can therefore be updated
final boolean canUpdate = (this == childView.getParent());
if (canUpdate) {
childView.setLayoutParams(params);
if (forceRedraw)
invalidate();
} else {
addView(childView, params);
}
}
}