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

为此,我在来自StaggeredGridLayoutManager的RecyclerView上使用了一个android.support.v7.widget。问题是StaggeredGridLayoutManager不支持wrap_content。
还有其他问题涉及这个问题,但它们涉及的是线性布局,而不是交错网格:
据我所知,我可以派生StaggeredGridLayoutManager并实现onMeasure。有没有办法做到这一点,而不重新计算的位置和大小的儿童本人?在查看StaggeredGridLayoutManager.java源时,我可以看到它使用ScrollbarHelper来近似滚动内容的大小。有什么方法可以重复使用吗?
发布于 2015-03-20 16:09:34
发布于 2015-11-21 20:37:39
问题是,在绘制RecyclerView时,它在绘制下一个元素之前计算出所有剩余的大小,并且在绘制其他元素后不重新计算,将它们留在屏幕之外。
对于这个问题,有一个简单的解决方法:诀窍是先绘制所有其他元素,然后将RecyclerView留到最后。使用相对布局,并将RecyclerView放在XML文件中。由于使用相对布局,您可以将每个元素放在任何您想要的位置,而不依赖于XML文件上的顺序,因此您将在RecyclerView之前绘制所有元素,这将使其计算出准确的剩余空间,wrap_content将正常工作。
在RecyclerView下面添加分页栏的示例:
<?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>https://stackoverflow.com/questions/29118146
复制相似问题