首页 > 解决方案 > 带全边框的 GridLayoutManager

问题描述

拥有一个带有 GridLayoutManager 的 RecyclerView。每行负责显示三个项目。预期的视图是,连同每个项目,都应该被完整的边框覆盖,没有额外的粗体(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()

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 是

但预期的用户界面如下(全覆盖边框,厚度相同)

网格中的期望

提前致谢

标签: androidandroid-recyclerviewbordergridlayoutmanager

解决方案


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

我想出了不同的方法(一种其他合乎逻辑的方法)来解决 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 类。这个项目装饰类期望垂直和水平可绘制以及 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)

在此处输入图像描述


推荐阅读