From 1811e31cdbce08ce59eb673fa64a43192006afd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Fri, 29 May 2015 17:23:37 +0200 Subject: 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 --- .../qtproject/qt5/android/QtActivityDelegate.java | 9 +++---- .../src/org/qtproject/qt5/android/QtLayout.java | 30 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtActivityDelegate.java b/src/android/jar/src/org/qtproject/qt5/android/QtActivityDelegate.java index ba3ecfecd6..b7190fd4de 100644 --- a/src/android/jar/src/org/qtproject/qt5/android/QtActivityDelegate.java +++ b/src/android/jar/src/org/qtproject/qt5/android/QtActivityDelegate.java @@ -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); } } diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtLayout.java b/src/android/jar/src/org/qtproject/qt5/android/QtLayout.java index 4d7ca47dde..408636dcf3 100644 --- a/src/android/jar/src/org/qtproject/qt5/android/QtLayout.java +++ b/src/android/jar/src/org/qtproject/qt5/android/QtLayout.java @@ -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); + } + } } -- cgit v1.2.3