首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >制作StaggeredGridLayout包装内容

制作StaggeredGridLayout包装内容
EN

Stack Overflow用户
提问于 2015-03-18 09:23:25
回答 2查看 1.6K关注 0票数 3

我需要在线性布局中显示交错网格。

为此,我在来自StaggeredGridLayoutManagerRecyclerView上使用了一个android.support.v7.widget。问题是StaggeredGridLayoutManager不支持wrap_content

还有其他问题涉及这个问题,但它们涉及的是线性布局,而不是交错网格:

据我所知,我可以派生StaggeredGridLayoutManager并实现onMeasure。有没有办法做到这一点,而不重新计算的位置和大小的儿童本人?在查看StaggeredGridLayoutManager.java源时,我可以看到它使用ScrollbarHelper来近似滚动内容的大小。有什么方法可以重复使用吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-20 16:09:34

票数 1
EN

Stack Overflow用户

发布于 2015-11-21 20:37:39

问题是,在绘制RecyclerView时,它在绘制下一个元素之前计算出所有剩余的大小,并且在绘制其他元素后不重新计算,将它们留在屏幕之外。

对于这个问题,有一个简单的解决方法:诀窍是先绘制所有其他元素,然后将RecyclerView留到最后。使用相对布局,并将RecyclerView放在XML文件中。由于使用相对布局,您可以将每个元素放在任何您想要的位置,而不依赖于XML文件上的顺序,因此您将在RecyclerView之前绘制所有元素,这将使其计算出准确的剩余空间,wrap_content将正常工作。

在RecyclerView下面添加分页栏的示例:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:id="@+id/pagination_btns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/previous_btn_label"/>
        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/next_btn_label"/>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/items_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_above="@id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR 


</RelativeLayout>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29118146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档