简体   繁体   English

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

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

在此处输入图片说明

I have created a ListView to which I've added CardView. 我创建了一个ListView,并向其中添加了CardView。

As you can see in the image, when I start scrolling, the last Card in the ListView is not completely displayed. 正如您在图像中看到的那样,当我开始滚动时,ListView中的最后一张Card并未完全显示。 It scrolls up but it does not come above Android's navigation bar at the bottom. 它向上滚动,但没有位于底部Android导航栏的上方。 I've been testing on a HTC One M8. 我一直在测试HTC One M8。

Here is the ListView: 这是ListView:

<?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>

And here is the CardView that I inflate and set into the adapter of the listview: 这是我充气并设置到列表视图适配器的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>

I have also tried my card view layout without wrapping it in a LinearLayout and it still does not work. 我还尝试了名片视图布局,而没有将其包装在LinearLayout中,但仍然无法正常工作。

Is there a setting in the layout xml that will take care of this? 布局xml中是否有可以解决此问题的设置?

Thanks in advance. 提前致谢。

 android:layout_marginBotton=" "

添加保证金底部应尝试一次。

So adding a margin to the bottom of listView worked, as below: 因此,在listView的底部添加一个边距是可行的,如下所示:

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

Am surprised this is the only way to get it to work though. 令人惊讶的是,这是使它正常工作的唯一方法。 I would have assumed the Android framework took care of these things. 我本来以为Android框架会处理这些事情。

Is the ListView in a Fragment ? ListView片段中吗? As far as I know, this is possible when you have used a Toolbar such as : 据我所知,当您使用诸如以下的Toolbar时,这是可能的:

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

then change the attribute to: 然后将属性更改为:

    app:layout_scrollFlags="enterAlwaysCollapsed"

Adding margin at the bottom of ListView is sort of a hack. 在ListView的底部添加页边距是一种技巧。 I have given a probable cause for this. 我为此给出了可能的原因。 If this is the case, then you won't need to add the margin in ListView. 如果是这种情况,则无需在ListView中添加边距。 There is always a reason behind the UI behavior. UI行为背后总有一个原因。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM