Android中SwipeRefreshLayout嵌套ListView最后一行被遮挡问题求助
问题:ListView最后一行被遮挡的解决方案
核心原因
你的布局里,ListView 和父容器 SwipeRefreshLayout 都设置了 match_parent 高度,但列表项(list_row.xml)的 MaterialCardView 带有 android:layout_marginBottom="8dp",最后一行的底部外边距会超出父容器范围,导致被截断。另外外层冗余的 LinearLayout 也可能干扰布局计算逻辑。
解决方法
方法一:给ListView添加底部内边距(推荐)
给 ListView 添加底部内边距,并开启 clipToPadding="false",让列表内容可以滚动到内边距区域,避免最后一行被裁剪:
<ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:listSelector="@android:color/transparent" android:divider="@null" android:dividerHeight="0dp" android:paddingBottom="8dp" android:clipToPadding="false"/>
方法二:给SwipeRefreshLayout添加底部外边距
直接给 SwipeRefreshLayout 设置底部外边距,留出容纳列表项底部间距的空间:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginBottom="8dp">
方法三:移除冗余布局层级
外层 RelativeLayout 里的 LinearLayout 完全多余,移除后可以简化布局结构,同时搭配上述任一方法使用:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginBottom="8dp"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:listSelector="@android:color/transparent" android:divider="@null" android:dividerHeight="0dp" android:paddingBottom="8dp" android:clipToPadding="false"/> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <com.google.android.material.textview.MaterialTextView android:id="@+id/empty" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/empty_list_text" /> </RelativeLayout>
内容的提问来源于stack exchange,提问作者Medi
相关产品推荐
相关产品推荐

