如何在Reclerview Android中填充项目宽度的子项 [英] How to fill item width children in reyclerview android

本文介绍了如何在Reclerview Android中填充项目宽度的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨,我在安卓工作。我想让孩子们都能看到回收站Android的全景。我有水平重影。我将在图表中显示我想要的内容。

场景1

当我在回收器视图中有更多项目时,我希望在我的回收器视图中显示这样的孩子。

预期产量

场景2

当我有三个项时,我想这样显示。它将填充循环视图中的整个视图。

预期产量

场景3

当我在回收视图中有两个项目时,我需要看起来像这样

预期产量

我得到的问题是我有3个项目,视图没有填满。实际上,我希望像场景2那样将整个视图拉伸为全宽。

实际产量

Item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/drawable_selector"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp" />

        <TextView
            android:id="@+id/subText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/tagContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="gone">

        <TextView
            android:id="@+id/tagText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingStart="10dp"
            android:paddingEnd="10dp" />

    </RelativeLayout>

</FrameLayout>

更新

在@Cheticamp建议之后,我执行了此代码

companion object {
        fun bindView(parent: ViewGroup): XYZViewHolder {
            val view = XyzLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            val lp = view.container.layoutParams
            lp.width = parent.measuredWidth / 3
            return OptionsViewHolder(
                view
            )
        }
    }

如您所见,我的最后一项是从末尾切下来的。

我认为在我的框架布局中我使用了空白处10 dp它是否会导致问题?如果你需要更多,请参考我的版面。还有一件事,我没有划分框架布局,而是将线性布局作为容器。我正在添加我的GitHublink

推荐答案

您可以在onCreateViewHolder()中更改回收视图项目视图的宽度。查看组父级是回收视图

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
    val lp = view.layoutParams
    // Change the width of the item view here
    lp.width = parent.measuredWidth / 3 // width is 1/3 of the width of the RecyclerView
    return ItemViewHolder(view)
}

这篇关于如何在Reclerview Android中填充项目宽度的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆