【问题标题】:GridLayoutManager with full border带全边框的 GridLayoutManager
【发布时间】:2019-12-30 16:04:05
【问题描述】:

拥有一个带有 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 如下所示(全覆盖边框,厚度相同)

提前致谢

【问题讨论】:

  • 这是因为当网格中的项目彼此相邻放置时,它会在某个地方占据彼此的宽度。到目前为止,您的方法是正确的,您只需要提供您的回收视图边框,以便它与您的项目边框匹配。请记住,您的回收站视图的边框宽度等于 @dimen/item_background_border_width。
  • @Abhishek 您能否解释一下我们如何按照您的建议为 recyclerview 提供边界..?
  • 不能直接给recyclerView应用边框。你可以通过放置一个适合 RecyclerView 布局的 FrameLayout 并将边框设置为该 FrameLayout 来实现这一点。明白我的意思了吗?

标签: android android-recyclerview border gridlayoutmanager


【解决方案1】:

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

我想出了不同的方法(一种其他合乎逻辑的方法)来解决 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2012-11-22
    • 2019-04-22
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多