如果在recycleView上方有其他视图,我永远无法弄清楚如何在底部正确限制recyclerview。我一定缺少一些琐碎的东西,因为这很令人发疯。
以下布局看起来不错:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
android:animateLayoutChanges="true"
android:background="@color/colorWhite"
tools:context=".alerts.AlertsActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/alerts_swiperefresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/alerts_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/md_keylines"
android:paddingTop="@dimen/md_keylines"
android:paddingRight="@dimen/md_keylines" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
现在,让我们在recycleview上方添加一个具有固定高度的View。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
android:animateLayoutChanges="true"
android:background="@color/colorWhite"
tools:context=".alerts.AlertsActivity">
<View
android:id="@+id/top"
android:layout_width="0dp"
android:layout_height="80dp"
android:background="#20000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/alerts_swiperefresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/top">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/alerts_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/md_keylines"
android:paddingTop="@dimen/md_keylines"
android:paddingRight="@dimen/md_keylines" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
现在,recyclerview不再正确地限制在底部,并且底部物品不可见或被部分切断。设计者还指出recyclerview的底部超出了屏幕下方的范围。
如果我用RelativeLayout或LinearLayout替换Constraintlayout,看起来很好。
从设计者的角度很容易看到差异。
将底部的constraint
添加到您的SwipeRefreshLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/alerts_swiperefresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/top">