【问题标题】:Want to add horizontal spacing between cards of CardView in android?想在android中的CardView卡片之间添加水平间距?
【发布时间】:2017-04-04 11:22:58
【问题描述】:

想要在 CardView 的卡片之间添加间距而不使用 cardUseCompatPadding,我该如何实现?

【问题讨论】:

  • 您可以使用填充。更多细节显示截图。
  • 你为什么不想使用cardUseCompatPadding??????
  • 关于使用 cardUseCompatPadding 如果我向左/向右滑动,背景不会占用该填充,并且它具有更多的高度和宽度

标签: android android-recyclerview android-cardview cardview


【解决方案1】:

在行上设置填充,以便所有项目设置每个项目之间的间距

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFF"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

// here your views

</LinearLayout>

或者,java 像这样使用来分离项目

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());  
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
    recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;

public SpacesItemDecoration(int space) {
    this.space = space;
}

@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
}

}

【讨论】:

    【解决方案2】:

    如果您使用数据绑定,您可能更愿意检查列表项中的&lt;Space /&gt; 组件水平RecyclerView 间距

    如下所示;

    <data>
    
        <variable
            name="data"
            type="com.foo.Entity" />
    
        <variable
            name="position"
            type="int" />
    
        <import type="android.view.View" />
    </data>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:gravity="center"
        android:orientation="horizontal">
    
        <Space
            android:layout_width="6dp"
            android:layout_height="6dp"
            android:visibility="@{position == 0 ? View.GONE : View.VISIBLE}" />
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/gray_border">
    
            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_margin="8dp"
                android:background="@color/white"
                android:gravity="center"
                android:lineSpacingExtra="4sp"
                android:maxLines="1"
                android:singleLine="true"
                android:text="@{data.text}"
                android:textColor="#6b778d"
                android:textSize="12sp"
                android:textStyle="normal"
                tools:text="Foo" />
    
        </RelativeLayout>
    </LinearLayout>
    

    你会看到的;

    【讨论】: