浮动操作按钮与最后一个卡片视图中的复选框重叠

Floating Action Button overlaps checkbox in last cardview

我有一个包含 RecyclerView 和 FloatingActionButton 的布局文件。在里面我有多个 CardViews。 主布局文件:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <include layout="@layout/toolbar"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/checkinput_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/checkinput_cv_top"
        android:layout_marginTop="8dp"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_arrow_forward_white_24dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/checkinput_btn_ready" />
</RelativeLayout>

卡片视图的 xml 布局:

<?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="wrap_content">
    <android.support.v7.widget.CardView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/cardview_checkinput_tv_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="8dp"
                android:text="Stadt:"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:layout_toStartOf="@+id/cardview_checkinput_cb" />

            <TextView
                android:id="@+id/cardview_checkinput_tv_input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/cardview_checkinput_tv_category"
                android:layout_alignStart="@+id/cardview_checkinput_tv_category"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginRight="8dp"
                android:text="TextView"
                android:layout_toStartOf="@+id/cardview_checkinput_cb" />

            <CheckBox
                android:id="@+id/cardview_checkinput_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true" />

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

问题是卡片视图的复选框位于卡片视图的右侧。 它看起来像这样:

如您所见,最后一个复选框不可见,因为 FloatingActionButton 与它重叠。问题是 recyclerview 中的 cardviews 计数每次都不相同。 cradviews 以编程方式添加,所以我不能只设置最后一个的 CheckBox 应该在 xml 文件中有 toStartOf -> FloatingActionButton。

所以我在适配器中以编程方式尝试了它:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.cb.getLayoutParams();
params.addRule(RelativeLayout.START_OF, R.id.checkinput_btn_ready);
holder.cb.setLayoutParams(params);

这对我不起作用,但我不知道如何将最后一个卡片视图的复选框移动到 FloatingActionButton 的左侧。

您是否尝试过获取适配器数量(然后获取最后一项)并在您的复选框中设置 margin-right?

将您的 recyclerview 代码更改为此

   <android.support.v7.widget.RecyclerView
        android:id="@+id/checkinput_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="100dp"
        android:clipToPadding="false"
        android:layout_below="@+id/checkinput_cv_top"
        android:layout_marginTop="8dp"/>

如果它不尝试增加 paddingbottom,它会在 fab 图标上方显示您的最后一个项目。