android - 带全边框的 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>
但预期的用户界面如下(全覆盖边框,厚度相同)
提前致谢
解决方案
要求是提供带有网格的内容集,每个内容的边框应与上面所附的预期屏幕截图相同。
我想出了不同的方法(一种其他合乎逻辑的方法)来解决 RecyclerView 的这个问题。在这里,我将描述我是如何实现这一目标的。
为 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 布局的外边框。
- 为了给子项提供边框,我们将使用 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>
为了访问 GridDividerItemDecoration,我们需要在 app 的 build.gradle 中添加依赖。
compile 'com.bignerdranch.android:simple-item-decoration:1.0.0'
输出截图附在下面(宽度为 0.3 dp)
推荐阅读
- php - 使用 php ajax 在数据库中插入和更新不同的表
- python - “权重总和为零,无法标准化”plotly.express 的树形图中的错误
- python - 如何根据输入文件扩展名更改 pd.read_excel() 引擎?
- django - 无法从 django 中的模板调用第二个视图
- c# - System.InvalidOperationException:无法解析类型的服务 - 依赖注入
- powershell - 在 Windows10 Shutdown 中使用 popupmessage 执行 powershell 脚本
- shopify - 我们如何在shopify中拆分文本?
- jquery - .NET Framework 4.6.2 无法命中 POST 端点
- r - 使用 r 中的 ggplot2 沿 y 轴订购气泡图气泡
- kubernetes-ingress - k8s 入口无法路由到服务