微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有一种方法可以在Android中使用StaggeredGridLayoutManager而不设置列/行大小?

如何解决有没有一种方法可以在Android中使用StaggeredGridLayoutManager而不设置列/行大小?

我在Android中有一个StaggeredGridLayoutManager,它与一个RecyclerView一起使用,我想容纳不同宽度的按钮(但是高度相同)。例如,请参见下面。

例如:I want the buttons to look like this

我希望按钮在进入下一个按钮之前要尽可能多地占用一行空间。因此,我无法将int spanCount设置为开头,因为我不知道需要占用多少行。我还需要保留按钮的顺序。

我查看了许多资源,例如hereherehere。但是,似乎在每个示例中,行大小或列大小都保持不变;但是,这不适用于我的任务,因为我需要使用很多行来填充所有按钮。我看到使用StaggeredGridLayoutManager的另一种方法是使用此

公共StaggeredGridLayoutManager(上下文上下文, AttributeSet属性, int defStyleAttr, int defStyleRes)

在Android StaggeredGridLayoutManager页面中,但是我看不到有关如何传递一组属性的示例。 StaggeredGridLayoutManager上的所有示例仅涉及设置跨度大小和方向。

作为参考,我有一个包含以下代码的类:

with(binding.recyclerViewName) {
layoutManager = StaggeredGridLayoutManager(mySpanCount,StaggeredGridLayoutManager.HORIZONTAL)
adapter = myAdapterName }


我的button.xml文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:clickable="false"
        android:importantForAccessibility="yes">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/margin_1_0"
            android:paddingEnd="@dimen/margin_1_0"
            android:clickable="true"
            android:background="@drawable/button_drawable"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

我没有任何让按钮显示在我的recyclerview上的问题。实际上,当我有4个按钮并且将跨度计数设置为3并将方向设置为水平时,某些按钮在第三行上组合在一起,但顺序不正确。另外,我不想对数字进行硬编码,而让代码找出如何按顺序排列按钮。

我需要为此任务创建一个自定义布局管理器吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。