所以我尝试重构我的一些回收的ViewHolders到ConstraintLayouts.我做完之后,在看到之后感到震惊.对单个视图进行充气所需的时间比平常多20倍LinearLayout.它实际上在执行时会跳过这么多帧.
编辑:约束布局的版本不相关.试过不同的组合有几乎相同的结果.
任何人都能解释为什么会这样吗?也许它不是为这种"沉重"的观点而设计的?
以下是XML用于以下内容的根ViewHolder:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:elevation="@dimen/param_2"
android:orientation="vertical"
android:stateListAnimator="@animator/material_selector">
<LinearLayout
android:id="@+id/order_view_tabs_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/param_2"
android:padding="@dimen/param_4"
android:background="@color/white"
android:divider="@drawable/empty_horizontal_divider"
android:elevation="@dimen/param_2"
android:orientation="horizontal"
android:showDividers="middle"
android:visibility="gone"/>
<include layout="@layout/order_list_item_constraint"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是 order_list_item_constraint.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/param_4"
android:clipToPadding="false">
<TextView
android:id="@+id/delivery_status"
style="@style/DefaultText.Normal"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="4dp"
android:padding="4dp"
android:background="@color/white"
android:elevation="2dp"
android:gravity="center_vertical"
android:text="@string/main_swipe_list_item_info_title_delivered_time"
app:layout_constraintEnd_toStartOf="@id/mid_guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="4dp"/>
<TextView
android:id="@+id/order_list_item_order_title"
style="@style/FullListItemInfoText"
android:layout_width="0dp"
android:layout_marginTop="4dp"
android:text="@string/main_swipe_list_item_info_title_order"
android:textColor="@color/red_900"
app:layout_constraintEnd_toEndOf="@id/mid_guideline"
app:layout_constraintStart_toStartOf="@id/start_guideline"
app:layout_constraintTop_toBottomOf="@+id/delivery_status"/>
<TextView
android:id="@+id/order_list_item_order_id"
style="@style/FullListItemInfoDetailsText"
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="@id/mid_guideline"
app:layout_constraintStart_toStartOf="@+id/start_guideline"
app:layout_constraintTop_toBottomOf="@id/order_list_item_order_title"
/> …Run Code Online (Sandbox Code Playgroud) xml android android-layout android-measure android-constraintlayout
为什么当您反复更改包含在父视图中的子视图的可见性并测量父视图时,Android 会返回错误的结果?
我创建了一个简单的测试:一个只有一个 ConstraintLayout 和两个 TextView 的 XML 文件。我会反复将最后一个 TextView 的可见性更改为 GONE 和 VISIBLE。每次我改变 TextView 的可见性时,我都会测量 ConstraintLayout 的宽度和高度。为了改变 TextView 的可见性,我在 ConstraintLayout 上设置了一个点击监听器。
这是上述简单布局的 XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/iWillShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/colorOnPrimary"/>
// This one will be hidden on click
<TextView
android:id="@+id/hideMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iWillShow"
android:textColor="@color/colorOnPrimary" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在我的 Kotlin 文件中,我有以下内容:
testLayout.setOnClickListener {
val matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec((it.parent as View).width, View.MeasureSpec.EXACTLY)
val wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) …Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-viewgroup android-measure
我遇到过两次或三次这种情况,我有一些看起来像这样的代码:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.requestLayout();
Run Code Online (Sandbox Code Playgroud)
或者:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
视图大小永远不会改变.我试过以下,没有运气:
forceLayout,具有讽刺意味的似乎不那么有力requestLayout,因为它没有通知View的父母需要布局.onMeasure()以查看是否曾被调用(它不是).requestLayout在布局过程中没有进行调用.我也尝试过调用requestLayout所有View的父母,如下所示:
ViewParent parent = myView.getParent();
while (parent != null) {
parent.requestLayout();
parent = parent.getParent();
}
Run Code Online (Sandbox Code Playgroud)
这有效,但看起来真的很酷.我更愿意找到真正的解决方案.
我究竟做错了什么?