当前位置: 代码迷 >> java >> 如何在垂直回收站视图上添加水平滚动?
  详细解决方案

如何在垂直回收站视图上添加水平滚动?

热度:66   发布时间:2023-07-31 11:17:02.0

所以我有一个这样的回收者视图:

如您所见,屏幕宽度不足以很好地显示所有文本,因此我需要添加水平滚动以便用户可以同时水平和垂直滚动,我该怎么做?

将 Horizo??ntalScrollView 与 LinearLayout 一起用作子项。 将它的 Orientation 设置为水平并将您的动态视图添加到它。

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

    <LinearLayout
            android:id="@+id/ll_main"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            // Add your dynamic views to this layout.
    </LinearLayout>

</HorizontalScrollView>

您可以在行布局中使用它:

<HorizontalScrollView 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:android="http://schemas.android.com/apk/res/android">

     //Your layout item here

</HorizontalScrollView>
  1. 您可以在 TextView 中添加 android:scrollHorizo??ntally="true"
  2. 如果列表项中有多个视图,则可以使用 Horizo??ntalScrollView

将垂直RecyclerView放在HorizontalScrollView如下所示。

<HorizontalScrollView
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tasks_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

</HorizontalScrollView>

此外, RecyclerView的项目视图的根元素的宽度必须为wrap_content而不是match_parent ,通过将其宽度设置为wrap_content它允许它扩展其父元素的宽度,在这种情况下将是RecyclerView

现在,在项目视图中,为内部视图提供wrap_content或固定宽度,在您的情况下,这些视图是显示“No”、“Wonum”、“Item num”、“Quantity”和“UOM”的视图。 通过提供固定的内部视图或wrap_content选项,它们将自动扩展或根据给定的宽度扩展,从而扩展其父级。

尝试这个

     val linearLayoutManager = LinearLayoutManager(this)
                linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
                selected_recycler_view.layoutManager = linearLayoutManager

adapter = TAdapter(this, existing.selectedArrayList)

使用此布局管理器允许在垂直 LinearLayoutManager 中水平滚动长项目。

import android.content.Context;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class FullScrollLayoutManager extends LinearLayoutManager {
    private int offset;
    private int maxOffset;

    public FullScrollLayoutManager(Context context) {
        super(context);
    }

    @Override
    public void onLayoutCompleted(RecyclerView.State state) {
        super.onLayoutCompleted(state);
        int n = getChildCount();
        offset = 0;
        maxOffset = 0;
        int ownWidth = getWidth();
        for(int i=0; i<n; ++i) {
            View view = getChildAt(i);
            int x = view.getRight();
            if(x>ownWidth) maxOffset = Math.max(maxOffset,x-ownWidth);
        }
    }

    @Override
    public boolean canScrollHorizontally() {
        return true;
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        if(dx<0) {
            if(-dx>offset) dx = -offset;
        }
        else
        if(dx>0) {
            if(dx+offset>maxOffset) dx = maxOffset-offset;
        }
        offsetChildrenHorizontal(-dx);
        offset += dx;
        return dx;
    }

}
  相关解决方案