如何在回收视图内均匀分配物品? [英] how to distribute items inside recycle-view evenly?

查看:88
本文介绍了如何在回收视图内均匀分配物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下回收视图:

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

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

and below how i set the items of above recycle view:

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

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

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

下面是我希望它水平和均匀分布的项目

and below is the item that i like to have it distribute horizontal and evenly

<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并将权重设置为1,但是,有了这个建议(上面的代码),我收到了一个很大的垂直空白空间(未显示文本视图),如下所示:

i read few article that said to put 0dp width and set weight as 1 but, with that suggestion (above code), i receive a big empty vertical blank space (Text view is not shown), like below:

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

如果我将宽度设置为包裹并去除重量,则显示为

if i set width as wrap and remove weight it is shown like this

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

Try this layout manager in place of linear layout manager, it will work for sure. This layout manager makes sure all items are evenly distributed like you need them to. https://gist.github.com/saurabhdhillon/d982627cb9296d0a2d00f1d664bdb8ac

信用: https://gist.github.com/heinrichreimer

这篇关于如何在回收视图内均匀分配物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆