RecyclerView将marginBottom添加到其项目中

时间:2016-01-18 21:24:02

标签: android android-recyclerview margins

我有一个RecyclerView,可以自动为其子节点添加marginBottom。 我已经尝试在XML中设置零边距并以编程方式设置0 marginBottom。 Android将这些边距设置为适配器的onBindView。 它就像你在图像中看到的那样。

enter image description here

编辑:这就是XML文件

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

    <android.support.v7.widget.CardView
        android:id="@+id/message_processo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#fff"
        app:cardCornerRadius="5dp"
        android:elevation="2dp"
        android:orientation="vertical"
        app:cardBackgroundColor="@color/white">

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

            <RelativeLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <br.com.isilist.android.view.widget.SquareImageView
                    android:id="@+id/iv_product_image"
                    android:src="@drawable/default_product_open_screen"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"/>

                <View
                    android:layout_below="@+id/iv_product_image"
                    android:layout_above="@+id/container_bottom"
                    android:id="@+id/middle_separator"
                    android:layout_width="match_parent"
                    android:layout_height="1px"/>

                <RelativeLayout
                    android:layout_below="@+id/iv_product_image"
                    android:id="@+id/container_bottom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:paddingLeft="5dp"
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/tv_name"
                            android:layout_marginLeft="10dp"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginRight="10dp"
                            android:layout_marginTop="20dp"
                            android:textSize="16sp"
                            android:text="Teste"
                            android:layout_marginBottom="@dimen/pad_5dp"
                            android:textColor="@color/dark_gray"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <RelativeLayout
                            android:layout_marginStart="10dp"
                            android:layout_marginLeft="10dp"
                            android:layout_marginBottom="4dp"
                            android:id="@+id/rl_sale"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@drawable/red_button">

                            <TextView
                                android:textSize="12sp"
                                android:textColor="#fff"
                                android:layout_margin="3dp"
                                android:text="@string/sale"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />

                        </RelativeLayout>

                        <TextView
                            android:id="@+id/tv_unit_price"
                            android:layout_marginLeft="10dp"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginRight="10dp"
                            android:textSize="13sp"
                            android:layout_marginBottom="@dimen/pad_5dp"
                            android:textColor="#888"
                            android:text="Teste"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/rl_bottom_layout_list_product">

                            <LinearLayout
                                android:id="@+id/ll_info_quantity_price"
                                android:layout_centerVertical="true"
                                android:orientation="vertical"
                                android:layout_marginBottom="8dp"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">

                                <View
                                    android:layout_marginLeft="8dp"
                                    android:layout_marginStart="@dimen/pad_8dp"
                                    android:layout_marginRight="@dimen/pad_8dp"
                                    android:layout_marginEnd="@dimen/pad_8dp"
                                    android:background="#888"
                                    android:layout_width="match_parent"
                                    android:layout_height="1px"/>

                                <TextView
                                    android:id="@+id/tv_item_quantity"
                                    android:textSize="13sp"
                                    android:textColor="#888"
                                    android:text="Quantidade: 12"
                                    android:layout_marginTop="@dimen/pad_4dp"
                                    android:layout_marginStart="10dp"
                                    android:layout_marginEnd="10dp"
                                    android:layout_marginLeft="10dp"
                                    android:layout_marginRight="10dp"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content" />

                                <LinearLayout
                                    android:orientation="horizontal"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content">

                                    <TextView
                                        android:textSize="13sp"
                                        android:textColor="@color/primary_color"
                                        android:text="R$"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginStart="10dp"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" />

                                    <TextView
                                        android:id="@+id/tv_item_total_price"
                                        android:textSize="@dimen/txt_18sp"
                                        android:textColor="@color/primary_color"
                                        android:text="148.99"
                                        android:layout_marginLeft="5dp"
                                        android:layout_marginStart="5dp"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" />

                                </LinearLayout>

                            </LinearLayout>
                        </RelativeLayout>


                    </LinearLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_marginBottom="-30dp"
                    android:layout_alignBottom="@+id/middle_separator"
                    android:id="@+id/ll_add_remove_item"
                    android:orientation="horizontal"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">


                    <RelativeLayout
                        android:layout_toLeftOf="@+id/container_add_quantity"
                        android:layout_toStartOf="@+id/container_add_quantity"
                        android:elevation="3dp"
                        android:layout_margin="@dimen/pad_16dp"
                        android:id="@+id/container_deduct_quantity"
                        android:background="@drawable/circle_background_orange_white_stroke"
                        android:layout_width="@dimen/pad_35dp"
                        android:layout_height="@dimen/pad_35dp">

                        <ImageView
                            android:src="@drawable/ic_minus_white_48dp"
                            android:layout_centerInParent="true"
                            android:layout_width="@dimen/pad_30dp"
                            android:layout_height="@dimen/pad_30dp" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:elevation="3dp"
                        android:layout_marginBottom="-30dp"
                        android:id="@+id/container_add_quantity"
                        android:layout_margin="@dimen/pad_16dp"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:background="@drawable/circle_background_orange_white_stroke"
                        android:layout_width="@dimen/pad_35dp"
                        android:layout_height="@dimen/pad_35dp">

                        <ImageView
                            android:src="@drawable/ic_plus_white_48dp"
                            android:layout_centerInParent="true"
                            android:layout_width="@dimen/pad_30dp"
                            android:layout_height="@dimen/pad_30dp" />
                    </RelativeLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/rl_badge"
                    android:layout_marginBottom="-12dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="@dimen/pad_16dp"
                    android:layout_alignBottom="@+id/middle_separator"
                    android:background="@drawable/circle_background_green"
                    android:layout_width="25dp"
                    android:layout_height="25dp">

                    <ImageView
                        android:id="@+id/iv_icon_badge_product"
                        android:src="@drawable/ic_check_white_48dp"
                        android:layout_centerInParent="true"
                        android:layout_width="20dp"
                        android:layout_height="20dp" />


                </RelativeLayout>

            </RelativeLayout>

        </RelativeLayout>

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

</LinearLayout>

有一种方法可以不让RecyclerView这样做吗?

1 个答案:

答案 0 :(得分:0)

此问题的两个来源可能是:

    android:dividerHeight=""
  • RecyclerView太大了。 (在这种情况下,最后一项没有底部边距。)

  • 项目本身有自己的底边。 (在这种情况下,所有项目都有底部边距。)