如何均匀分配回收视图中的项目?

问题描述 投票:1回答:2

我有回收视图如下:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:textAlignment="center" />

        <!-- items inside below RecycleView require to distribute evenly -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <!-- items inside above RecycleView require to distribute evenly -->

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

以下我如何设置上述回收视图的项目:

我尝试使用线性布局尽可能多(除非你告诉我这是不可能的)

LinearLayoutManager linearLayout = new LinearLayoutManager(mBinding.getRoot().getContext(), LinearLayout.HORIZONTAL, false);
mBinding.myRecycle.setLayoutManager(linearLayout);

myAdapter = new Adapter();
mBinding.myRecycle.setAdapter(myAdapter);

以下是我喜欢让它水平均匀分布的项目

<layout xmlns:android="http://schemas.android.com/apk/res/android" >

    <data> <variable name="obj" type="com.myapp.test.model.entity.SomeData"/> </data>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="4dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>
    </LinearLayout>
</layout>

我读了一些文章,说要把0dp宽度和设置权重设为1但是,有了这个建议(上面的代码),我收到一个很大的空垂直空格(文本视图没有显示),如下所示:

+-----------------------------------+
|                                   |
|                                   |
|                                   |
|                                   |
|                                   |
|                                   |
+-----------------------------------+
| the second recycle view           |
| goes here                         |
+-----------------------------------+

如果我将宽度设置为换行并移除重量,则显示如下

+-----------------------------------+
|tv10|tv20|tv30|tv40|               |
|tv11|tv21|tv31|tv41|               |
+-----------------------------------+
| the second recycle view           |
| goes here                         |
+-----------------------------------+

虽然我想要的是这样的:

+-----------------------------------+
|  tv10  |  tv20  |  tv30  |  tv40  |
|  tv11  |  tv21  |  tv31  |  tv41  |
+-----------------------------------+
| the second recycle view           |
| goes here                         |
+-----------------------------------+
android layout android-recyclerview android-linearlayout
2个回答
1
投票

尝试使用此布局管理器代替线性布局管理器,它肯定会起作用。此布局管理器可确保所有项目均匀分布,就像您需要的那样。 https://gist.github.com/saurabhdhillon/d982627cb9296d0a2d00f1d664bdb8ac


0
投票

更新

试试这个。你应该将'LinearLayout'作为项目布局的根视图

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.