美文网首页
ConstraintLayout 设置layoutParams在

ConstraintLayout 设置layoutParams在

作者: Ella_Eric | 来源:发表于2020-08-29 14:18 被阅读0次

ConstraintLayout 设置layoutParams在scrollview中不起作用

运行ConstraintLayout, 并将其放到ScrollView里面,但是设置layout_height 不起作用,ConstraintLayout的height总是和ScrollView一样,在xml和在代码里面设置均不起作用,stack overflow上也有人遇到一样的问题 stackoverflow 链接, 这应该是ConstraintLayout的一个bug, 解决方法就是设置minHeight或者maxHeight让height变成自己想要的高度。

<ScrollView 

    android:layout_width="match_parent"

    android:layout_height="300dp">

    <androidx.constraintlayout.widget.ConstraintLayout 

         android:layout_width="match_parent" 

         android:background="#FFC0CB" 

         android:minHeight="500dp" **

         android:layout_height="500dp" > 

         <TextView android:id="@+id/new_realm_message" 

             android:layout_width="wrap_content" 

             android:layout_height="wrap_content" 

             android:layout_marginTop="301dp" 

             android:text="long text" 

             app:layout_constraintBottom_toBottomOf="parent" 

             app:layout_constraintLeft_toLeftOf="parent" 

             app:layout_constraintRight_toRightOf="parent" 

             app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

recyclerView在scrollview中bind所有items

比如以下layout布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:minHeight="1000dp">
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            app:layout_constraintTop_toBottomOf="@id/text"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

以上代码中recyclerView的height是0dp, 打印log会发现他在render的时候adapter会调用itemCount次onBindViewHolder(), 如果itemcount特别大,有几千甚至上万个,会发现页面特别卡,没相应。 解决的办法就是设置一个具体的高度在recyclerview上面,像下面这样, 把0dp改成800dp:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="1000dp"
        android:minHeight="1000dp">
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            app:layout_constraintTop_toBottomOf="@id/text"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="800dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

相关文章

网友评论

      本文标题:ConstraintLayout 设置layoutParams在

      本文链接:https://www.haomeiwen.com/subject/pbtjsktx.html