【问题标题】:Android ListView with CardView: Last Card not fully displayed when scrolling带有 CardView 的 Android ListView:滚动时最后一张卡片未完全显示
【发布时间】:2015-10-09 06:38:20
【问题描述】:

我创建了一个 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 中是否有一个设置可以解决这个问题?

提前致谢。

【问题讨论】:

  • 在卡片视图的底部添加边距。
  • listview添加一些顶部和底部边距
  • 这是标准的做法吗?似乎很奇怪,Android 无法识别其在列表中的剪辑部分。

标签: android android-layout listview android-listview android-cardview


【解决方案1】:
 android:layout_marginBotton=" "

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

【讨论】:

    【解决方案2】:

    所以在 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 框架会处理这些事情。

    【讨论】:

      【解决方案3】:

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

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

      然后将属性改为:

          app:layout_scrollFlags="enterAlwaysCollapsed"
      

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

      【讨论】:

        猜你喜欢
        • 2021-07-07
        • 1970-01-01
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多