Android ListView 和 CardView:滚动时最后一张卡片未完全显示

Android ListView with CardView: Last Card not fully displayed when scrolling

我创建了一个 ListView,并向其中添加了 CardView。

如图所示,当我开始滚动时,ListView 中的最后一个 Card 没有完全显示。它会向上滚动,但不会超出 Android 底部的导航栏。我一直在 HTC One M8 上进行测试。

这是列表视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/wanna_list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:divider="@null"/>
</LinearLayout>

这是我膨胀并设置到列表视图适配器中的 CardView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/cv"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        card_view:cardCornerRadius="6dp"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="2dp">
    <!-- compat padding is required if using AppCompat -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp" >

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/thumbnail"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                android:src="@drawable/india"
                android:scaleType="centerCrop" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/firstLine"
                android:layout_toRightOf="@+id/thumbnail"
                android:layout_alignParentTop="false"
                android:textSize="20sp"
                android:text="Tennis with Peter"
                android:layout_alignTop="@+id/thumbnail" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/secondLine"
                android:layout_below="@+id/firstLine"
                android:text="10 Oct at 7pm"
                android:layout_alignLeft="@+id/firstLine" />

            <Button
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:text="YES"
                android:id="@+id/button3"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignLeft="@+id/thumbnail" />

            <Button
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:text="NO"
                android:id="@+id/button4"
                android:layout_toRightOf="@+id/button3"
                android:layout_alignBottom="@+id/button3"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="false" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

我也试过我的卡片视图布局,但没有将它包装在 LinearLayout 中,但它仍然不起作用。

布局中是否有设置 xml 可以解决这个问题?

提前致谢。

 android:layout_marginBotton=" "

添加边距底部应该尝试一次。

所以在 listView 的底部添加边距成功了,如下所示:

<ListView
        android:id="@+id/wanna_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:layout_marginBottom="80dp" />

我很惊讶这是让它工作的唯一方法。我本以为 Android 框架会处理这些事情。

ListView 是否在 片段 中?据我所知,当您使用 Toolbar 时,这是可能的,例如:

<android.support.v7.widget.Toolbar
        ...
        app:layout_scrollFlags="scroll"
        ...
          >
</android.support.v7.widget.Toolbar>

然后将属性更改为:

    app:layout_scrollFlags="enterAlwaysCollapsed"

在 ListView 的底部添加边距有点像 hack。我已经给出了一个可能的原因。如果是这种情况,则不需要在 ListView 中添加边距。 UI 行为背后总是有原因的。希望对您有所帮助。