android - 如何均匀分配回收 View 中的项目?

标签 android layout android-recyclerview android-linearlayout

我有回收 View 如下:

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

下面是我如何设置上述回收 View 的项目:

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

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,但是, 有了那个建议(上面的代码),我收到一个很大的垂直空白空间(未显示 TextView ),如下所示:

+-----------------------------------+
|                                   |
|                                   |
|                                   |
|                                   |
|                                   |
|                                   |
+-----------------------------------+
| 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                         |
+-----------------------------------+

最佳答案

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

致谢:https://gist.github.com/heinrichreimer

关于android - 如何均匀分配回收 View 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48852469/

相关文章:

android - 自定义 PreferenceScreen 的布局

ruby-on-rails - 如何将变量传递给布局?

android - onChildDraw 使项目闪烁

android-studio - RecyclerView( Kotlin ): Adding an swipe to delete functionality on a ToDo app with data in SQL database

java - android: runOnUiThread 中的变量

android - 奇怪 - Android IntentService 杀死了我应用程序的其余部分,但继续自行运行

javascript - 修复底部的div。 (不使用javascript)

android - 从回收者 View 中单击的项目获取值(value)

android - 如何在 ListView 上使用 Button 的 onClickListener 方法

android MVP - 我可以为自定义 View 和 fragment 设置多个演示者吗