使用RecycleView时,最后一个item无法展示完全,误以为是与NestScollView嵌套冲突

171 阅读1分钟

标题描述不够准确,因为一开始是使用Nest嵌套ConstrainLayout(cstl),在cstl中进行多控件布局(包含RecycleView和其他控件),此时出现RecycleView的item只能展示一部分,或最后一个控件只展示一部分,并且item里的内容会部分扭曲。误认为是与NestScollView滑动冲突。
对RecycleView增加了例如如下代码的操作,均无效

//            setHasFixedSize(true)
//            isNestedScrollingEnabled = false
//            focusable = NOT_FOCUSABLE

首次布局如下:

<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/sw_6dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/iv_fifteen_logo">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_weather_fifteen"
            android:layout_width="0dp"
            android:layout_height="@dimen/sw_300dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/bk_item_weather15day_list"
            tools:itemCount="2"/>

        <LinearLayout
            android:id="@+id/ll_personal_15day_more"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/rv_weather_fifteen">

            <TextView
                android:id="@+id/tv_15day_list_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="查看15天天气"
                android:textColor="#ff686868"
                android:textSize="12dp" />

            <ImageView
                android:id="@+id/iv_15day_down"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="4dp"
                android:src="@mipmap/iv_arrow_down" />

            <ImageView
                android:id="@+id/iv_15day_up"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="4dp"
                android:src="@mipmap/iv_arrow_up"
                android:visibility="gone" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/fl_ad2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/sw_19dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/ll_personal_15day_more" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

之后修改嵌套布局为RecycleView 多item类型布局,布局如下:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_weather_fifteen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/iv_fifteen_logo"
    tools:itemCount="2"
    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/bk_item_weather15day_list" />

只保留了RecycleView,将其余控件作为item布局。然而运行发现最后一个控件永远展示不全,懵逼中。。。这样已经排除了是嵌套NestScollView导致的滑动冲突,然后再次修改layout_height为0dp,增加layout_constraintBottom_toBottomOf="parent"属性。。布局如下:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_weather_fifteen"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/iv_fifteen_logo"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:itemCount="2"
    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/bk_item_weather15day_list" />

再次运行,状况意外的惊喜,,,最后一个item展示出来了。
第一次布局样式因为最外层也是ConstraintLayout,所以应该对Nest添加layout_constraintBottom_toBottomOf="parent"属性,将其限制在屏幕内。对于其他布局一般都使用match_parent直接限制高度在屏幕内。这样就可以在屏幕内完全展示。