how to distribute items inside recycle-view evenly?

♀尐吖头ヾ 提交于 2021-02-10 18:17:30

问题


i Have recycle view as follow:

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

I tried to be as much as possible using linear layout (unless you tell me that is not possible)

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>

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

while what i want is like this:

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

回答1:


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

Credits: https://gist.github.com/heinrichreimer




回答2:


UPDATED

try this.You should make 'LinearLayout' as root view of your item layout

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


来源:https://stackoverflow.com/questions/48852469/how-to-distribute-items-inside-recycle-view-evenly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!