带全边框的 GridLayoutManager

GridLayoutManager with full border

有一个 RecyclerView 和 GridLayoutManager。每行负责显示三个项目。预期的视图是,连同每个项目,应该用完整的边框覆盖,没有额外的粗体(4 边等宽)。 回收者视图的代码。无论我尝试过哪种方法,相应的 UI 屏幕也会附加。

    mHomeRecyclerView = findViewById(R.id.home_screen_recycler_view);
    mHomeAdapter = new HomeScreenAdapter(this, mHomeScreenItem);

    // to provide scrolling functionality to parent
    mHomeRecyclerView.setNestedScrollingEnabled(false);
    mHomeRecyclerView.setHasFixedSize(true);
    GridLayoutManager layoutManager = new GridLayoutManager(this,3);
    mHomeRecyclerView.setLayoutManager(layoutManager);
    mHomeRecyclerView.setAdapter(mHomeAdapter);

适配器的 onCreateViewHolder() class

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
RecyclerView.ViewHolder {
    mLayoutInflater = LayoutInflater.from(mContext)
    return HomeItemViewHolder(mLayoutInflater.inflate
    (R.layout.layout_home_child_item, parent, false))
}

layout_home_child_item.xml code is below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child_item_parent"
    android:layout_width="match_parent"
    android:layout_height="@dimen/one_not_seven_dp"
    android:padding="@dimen/image_select_tag_layout_padding"
    android:background="@drawable/shape_sub_category"
    android:clickable="true"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/child_item_image"
        android:layout_width="@dimen/each_item_dimen"
        android:layout_height="@dimen/each_item_dimen" />

    <TextView
        android:id="@+id/child_item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/image_icon_in_margin"
        android:textSize="@dimen/small_text_size"
        android:textColor="@color/result_received_icon_color"
        android:maxLines="1"
        android:ellipsize="end"
        android:gravity="center" />

</LinearLayout>

shape_sub_category.xml 如下所示

<item android:id="@android:id/mask">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimaryDark" />
        <corners android:radius="@dimen/search_card_margin" />
    </shape>
</item>

<item android:id="@android:id/background">
    <shape android:shape="rectangle">
        <stroke
            android:width="@dimen/item_background_border_width"
            android:color="@color/grid_border_clr" />
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

但预期的 UI 如下所示(全覆盖边框,厚度相同)

提前致谢

要求是提供一组带网格的内容,每个内容的边框应与上面所附的预期屏幕截图的粗细相同。

我想出了不同的方法(一种其他逻辑方法)来解决 RecyclerView 的这个问题。在这里,我描述了我是如何实现的。

  1. 为 RecyclerView 创建完整的外边框,在 RecyclerView 的布局下方和相应的可绘制边框。

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_screen_recycler_view"
        android:background="@drawable/recycler_border"
        android:layout_marginTop="@dimen/send_cmd_round_rect"
        android:layout_marginLeft="@dimen/send_cmd_round_rect"
        android:layout_marginRight="@dimen/send_cmd_round_rect"
        android:layout_marginBottom="@dimen/send_cmd_round_rect"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
    

recycler_border.xml 如下图

<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="@dimen/item_background_border_width"
        android:color="@color/grid_border_clr" />
    <solid android:color="@android:color/transparent" />
</shape>

通过上面的代码更改,我们可以观察到 RecyclerView 布局的外边框。

  1. 为了给子项提供边框,我们将使用 GridDividerItemDecoration class。此项目装饰 class 需要垂直和水平可绘制对象以及 RecyclerView 网格中的列数。

Drawable horizontalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider); Drawable verticalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider); mHomeRecyclerView.addItemDecoration(new GridDividerItemDecoration(horizontalDivider, verticalDivider, 3));

line_divider 是用于提供线规格的自定义绘图,如下所示。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
    android:width="@dimen/item_background_border_width"
    android:height="@dimen/item_background_border_width" />
<solid android:color="@color/grid_border_clr" /></shape>
  1. 为了访问GridDividerItemDecoration,我们需要在app的build.gradle.

    中添加依赖

    compile 'com.bignerdranch.android:simple-item-decoration:1.0.0'

    下面附上输出截图(宽度为0.3 dp)