NestedScrollView嵌套ConstraintLayo

2020-04-24  本文已影响0人  CreScert

如果NestedScrollView嵌套RecyclerView,这个我一直用下面三句话:

        rvList.setNestedScrollingEnabled(false);
        rvList.setHasFixedSize(true);
        rvList.setFocusable(false);

如果用约束布局ConstraintLayout,就还是会出现显示不全的问题,只会显示一行。即便是用requestLayout都不行,计算条目高度没试过,没必要那么麻烦。

处理办法是在RecyclerView外边用一个布局包住,我是用的RelativeLayout包住的。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

...
  <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cl_title">

       <androidx.constraintlayout.widget.ConstraintLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="@dimen/dp_20"
                  android:paddingLeft="@dimen/dp_15"
                  android:paddingRight="@dimen/dp_15">

                  ....
                  <!-- 此处用RelativeLayout 包住。RelativeLayout里面的属性从RecyclerView拷贝出来的。 --->
                  <RelativeLayout 
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/dp_18"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/tv_text3"
                        app:layout_constraintWidth_percent="1">

                       <androidx.recyclerview.widget.RecyclerView
                                  android:id="@+id/rv_list_grid"
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"/>
                 </RelativeLayout>
                  ...

      </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
...
</androidx.constraintlayout.widget.ConstraintLayout >
上一篇下一篇

猜你喜欢

热点阅读