可滚动的RecyclerView横向和纵向布局

4
当我使用ScrollView和HorizontalScrollView使水平和垂直滚动都成为可能,并切换到横向时,RecyclerView不像纵向方向那样填充屏幕。
我在一行布局中使用元素的相对宽度,在纵向方向上一切看起来都很好。
片段布局。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="horizontal"
    android:scrollbars="vertical"
    android:fillViewport="true">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_border">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/tableView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="horizontal"
            android:background="@drawable/table_border"
            tools:listitem="@layout/table_inventory_row"
            />
    </HorizontalScrollView>

</ScrollView>

一个行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:weightSum="1"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/txtId"
        android:layout_weight=".15"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:text="@string/id"
        android:textSize="18sp" />
    <TextView
        android:id="@+id/txtManager"
        android:layout_width="0dp"
        android:layout_weight=".25"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:text="@string/manager"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtCreatedDate"
        android:layout_width="0dp"
        android:layout_weight=".25"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:text="@string/created_on"
        android:textAlignment="center"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtExecutionDate"
        android:layout_width="0dp"
        android:layout_weight=".25"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:text="@string/executed_on"
        android:textAlignment="center"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtDone"
        android:layout_width="0dp"
        android:layout_weight=".1"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:text="@string/is_done"
        android:textAlignment="center"
        android:textSize="18sp" />
</LinearLayout>

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.
                    from(parent.getContext()).
                    inflate(R.layout.table_inventory_row, parent, false);
            return new RowViewHolder(itemView);

    }

风景 https://imgur.com/U2Vzwuh

肖像 https://imgur.com/5VaEJ6S https://imgur.com/97WGJ1n

如您在截图中所见,我已将蓝色边框放置于水平滚动条上,并且它占据了整个屏幕,但是RecyclerView的宽度不匹配。

我该如何使RecyclerView占据父元素的整个宽度?

2个回答

3
你的问题是如何创建每一行。你得到了你所请求的完全相同大小的行。当你处于竖屏模式时,你的回收视图比屏幕宽度大。然而,在横屏模式下,你的行的大小确实相同,但它没有覆盖整个屏幕宽度。
你应该使用"wrap_content"来设置文本视图的宽度,而不是设置权重。当你设置权重时,你会创建一个静态大小。使用"wrap_content"可以包装所有的内容。
此外,在你的fragment中,确保在旋转时创建你的视图的新实例:
Fragment.setRetainInstance(false); 

我考虑过这个问题,但我想做一个表格,如果这样做我的列宽度将不相同。但是,我仍然不明白为什么回收站视图会将内容换行,而不是与父元素宽度匹配? - Milorad
你能发一下你的片段代码吗?你也可以在元素上设置最小宽度。 - BlackHatSamurai

1

首先,准备好你的RecyclerView布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">
....

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layoutAnimation="@anim/layout_animation" />

        </HorizontalScrollView>

 .....


</RelativeLayout>

然后,添加

 android:configChanges="orientation|screenSize"

回到你的Manifests.xml

这样就可以从activity/fragment中设置好你的recyclerview了。

如果需要更多细节,这里有一篇文章供您参考。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接