如何在recyclerView android中填充项目宽度子项?

发布于 2025-01-12 03:24:35 字数 3816 浏览 4 评论 0原文

嘿我在安卓工作。我想让孩子们适应 recyclerview android 的整个视图。我有水平回收视图。我将在图表中展示我想要的内容。

场景1

当我在回收视图中有更多项目时,我想在我的回收视图中向孩子们展示这样的内容。

预期输出

在此处输入图像描述

场景 2

当我有 三个 项目时我想像这样显示。它将填充 reyclerview 中的整个视图。

预期输出

在此处输入图像描述

场景 3

当我在 reyclerview 中有两个项目时,我需要看起来像这样

预期输出

在此处输入图像描述

我遇到的问题是我有 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
            )
        }
    }

正如你可以看到我的最后一个项目是从 结尾。

输入图片此处描述

我认为在我的框架布局中我使用了 ma​​rginEnd 10 dp 是否会导致问题?如果您需要更多,请参考我的布局。还有一件事我没有划分框架布局,而是将线性布局作为容器。我正在添加我的 github 链接

Hey I am working in android. I want to fit children in whole view of recyclerview android. I have horizontal recylerview. I will show what I want in diagram.

Scenario 1

when I have more item in recyclerview, I want to show children like this in my recycler view.

Expected Output

enter image description here

Scenario 2

when I have three item I want to show like this. It will fill whole view in reyclerview.

Expected Output

enter image description here

Scenario 3

When I have Two item in reyclerview, I need to look like this

Expected Output

enter image description here

The problem I am getting that I have 3 item, the view is not filling fully. Actually I want to stretch whole view to full width like Scenario 2.

Actual output

enter image description here

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>

UPDATE

after @Cheticamp suggestion I did this code

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
            )
        }
    }

As you can see my last item is cut from the end.

enter image description here

I think in my framelayout i Used marginEnd 10 dp is it causing issue? please refer my layout if you need more. And one more thing I didn't divide framelayout instead I take linear layout as container. I am adding my github link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哑剧 2025-01-19 03:24:35

您可以在 onCreateViewHolder() 中更改 RecyclerView 项视图的宽度。 ViewGroup 父级是 RecyclerView

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)
}

You can change the width of the RecyclerView item views in onCreateViewHolder(). The ViewGroup parent is the RecyclerView.

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)
}
原来是傀儡 2025-01-19 03:24:35

自定义frameLayout类
使一个类继承 FrameLayout 或您使用的其他布局。

    public class SquareFrameLayout extends FrameLayout {
    public SquareFrameLayout(@NonNull Context context) {
        super(context);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

onMeasure方法很重要。

然后将根布局项recyclerView更改为SquareFrameLayout(现在构建的类)
像这样:

<com.example.app.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_rv_item"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_margin="4dp"
    xmlns:tools="http://schemas.android.com/tools">

   
   //your items

</com.example.app.SquareFrameLayout>

Customize frameLayout class
make a class inherit FrameLayout or another layout you use.

    public class SquareFrameLayout extends FrameLayout {
    public SquareFrameLayout(@NonNull Context context) {
        super(context);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

onMeasure method is important.

then change root layout item recyclerView to SquareFrameLayout (The class built now)
like this:

<com.example.app.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_rv_item"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_margin="4dp"
    xmlns:tools="http://schemas.android.com/tools">

   
   //your items

</com.example.app.SquareFrameLayout>
樱花细雨 2025-01-19 03:24:35

您可以使用 Google 提供的 flexbox-layout 库。
您可以根据需要调整项目。
下载该应用程序并查看此演示是否适合您。 https://github.com/google/flexbox-layout

You can use flexbox-layout library provided by Google.
You can adjust the items according to your need.
Download the app and see this demo working for you or not. https://github.com/google/flexbox-layout

简美 2025-01-19 03:24:35

按如下方式更新您的适配器:

class CustomAdapter(
    private val items: List<Item>, 
    private val screenWidth: Int
) : RecyclerView.Adapter<CustomAdapter.CustomViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return CustomViewHolder(view)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val itemWidth = calculateItemWidth()
        holder.itemView.layoutParams = RecyclerView.LayoutParams(itemWidth, RecyclerView.LayoutParams.MATCH_PARENT)
        holder.bindView(items[position])
    }

    override fun getItemCount(): Int = items.size

    private fun calculateItemWidth(): Int {
        return screenWidth / items.size
    }

    class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindView(item: Item) {
            
        }
    }
}

在您的活动/片段中,

val screenWidth = Resources.getSystem().displayMetrics.widthPixels
val adapter = CustomAdapter(items, screenWidth)

recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = adapter

Update your adapter as follow:

class CustomAdapter(
    private val items: List<Item>, 
    private val screenWidth: Int
) : RecyclerView.Adapter<CustomAdapter.CustomViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return CustomViewHolder(view)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val itemWidth = calculateItemWidth()
        holder.itemView.layoutParams = RecyclerView.LayoutParams(itemWidth, RecyclerView.LayoutParams.MATCH_PARENT)
        holder.bindView(items[position])
    }

    override fun getItemCount(): Int = items.size

    private fun calculateItemWidth(): Int {
        return screenWidth / items.size
    }

    class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindView(item: Item) {
            
        }
    }
}

In your activity/fragment,

val screenWidth = Resources.getSystem().displayMetrics.widthPixels
val adapter = CustomAdapter(items, screenWidth)

recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = adapter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文