如何在android中删除RecyclerView适配器上的多个背景边框?

时间:2018-03-12 11:53:47

标签: android android-recyclerview android-adapter gridlayoutmanager recyclerview-layout

我正在使用RecyclerView实现GridLayoutManager spanCount 3并且问题是多次显示的背景边框看屏幕截图和布局。但我需要在此适配器上使用行项单边框形状。所以分享您的经验并提前致谢!

enter image description here

这是RecyclerView.Adapter row xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linSubCat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@drawable/shape_sub_category"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">

<ImageView
    android:id="@+id/subcategory_image_listview"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center" />

<TextView
    android:id="@+id/subcategory_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="3dp"
    android:gravity="center|center_vertical"
    android:padding="3dp"
    android:textColor="@color/category_color" /></LinearLayout>

背景边框shape_sub_category.xml文件。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
    android:width="0.5dp"
    android:color="@color/category_color" />
<solid android:color="@android:color/transparent" /></shape>

1 个答案:

答案 0 :(得分:1)

您可以使用ItemDecoration

https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html

https://gist.github.com/lapastillaroja/858caf1a82791b6c1a36

Refer Here

样品

int numColumns = 2;

Drawable horizontalDivider = ContextCompat.getDrawable(this, R.drawable.divider_horizontal);
Drawable verticalDivider = ContextCompat.getDrawable(this, R.drawable.divider_vertical);

recyclerView.setLayoutManager(new GridLayoutManager(..., numColumns));

recyclerView.addItemDecoration(new GridDividerItemDecoration(horizontalDivider, verticalDivider, numColumns));

水平:divider_horizo​​ntal.xml

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#FF0000" />

垂直:divider_vertical.xml

<View
    android:layout_width="1dp"
    android:layout_height="fill_parent"
    android:background="#FF0000" />