Android 可滚动回收视图水平和垂直景观

Android 可滚动回收视图水平和垂直景观,android,android-recyclerview,Android,Android Recyclerview,当我使用ScrollView和HorizontalScrollView使水平和垂直滚动成为可能并切换到横向时,RecyclerView不会像纵向那样填充屏幕 我在一行布局中使用了元素的相对宽度,所有东西在纵向方向上看起来都很好 片段布局 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to

当我使用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>
景观

肖像画

正如你在截图中看到的,我在水平滚动条上加了蓝色边框,它占据了整个屏幕,但RecyclerView与宽度不匹配


如何使RecyclerView占据父级的全部宽度?

您的问题是如何创建每一行。你得到的正是你所要求的。这些行的大小都相同。在纵向模式下,回收器视图大于屏幕宽度。但是,在横向模式下,行的大小完全相同,但它不能覆盖整个屏幕宽度

您应该在文本视图宽度上使用
wrap\u content
,而不是设置权重。设置权重时,将创建静态尺寸。使用
wrap\u内容
可以包装所有内容

此外,在片段中,通过执行以下操作确保在旋转时创建视图的新实例:

Fragment.setRetainInstance(false); 

首先根据需要准备回收视图

<?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>
到您的Manifests.xml

以上就是从activity/fragment设置recyclerview的全部内容


关于更多细节,这里有一个给你的例子。

我想到了这一点,但我正在尝试制作一个表,如果我这样做,我的列将不会具有相同的宽度。但是,我仍然不明白为什么recycler视图包装内容而不是在宽度上匹配父级?你能发布你的片段代码吗?也可以在元素上设置最小宽度。
<?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"