【问题标题】:Scrollable Recyclerview Horizontal and Vertical LandscapeScrollable Recyclerview 水平和垂直横向
【发布时间】:2019-08-06 19:38:37
【问题描述】:

当我使用 ScrollView 和 Horizo​​ntalScrollView 使水平和垂直滚动成为可能并切换到横向时,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/5VaEJ6Shttps://imgur.com/97WGJ1n

正如您在屏幕截图中看到的那样,我已经为水平滚动条添加了蓝色边框,它占据了整个屏幕,但 RecyclerView 与宽度不匹配。

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

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

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

    您应该在文本视图宽度上使用wrap_content,而不是设置权重。当您设置权重时,您正在创建一个静态大小。使用wrap_content,您将包装所有内容。

    另外,在你的片段中,确保你在旋转时创建了一个新的视图实例:

    Fragment.setRetainInstance(false); 
    

    【讨论】:

    • 我想到了,但我正在尝试制作一张桌子,如果我这样做,我的列的宽度将不一样。但是,我仍然不明白为什么回收器视图会包装内容而不是在宽度上匹配父级?
    • 你能发布你的片段代码吗?您还可以在元素上设置最小宽度。
    【解决方案2】:

    首先准备你的 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

    这就是从活动/片段设置您的回收站视图。

    如需了解更多详情,请联系Article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2016-01-20
      • 2020-12-02
      • 2015-11-30
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多