【问题标题】:Make GridLayoutManager VERTICAL orientation with horizontal scrolling使用水平滚动使 GridLayoutManager 垂直方向
【发布时间】:2017-12-20 16:18:11
【问题描述】:

我需要具有水平滚动行为的 RecyclerView Gridlayout 管理器。它必须像垂直方向一样添加子视图(例如,(第 1 行,第 1 列)= 索引 0,(第 1 行,第 2 列)= 索引 1 等的列索引。

目前垂直方向不允许水平滚动并适合设备宽度的子视图。

编辑:

我在 RecyclerView GridLayoutManager 中有 100 行和 150 列,需要水平滚动视图。我正在隐藏和显示行,这只有在垂直方向上才有可能。

【问题讨论】:

  • 当您创建一个新的资源布局时,您会获得语言、方向、屏幕大小等限定符。在您的情况下,方向是其中之一。在清单中,您可以强制定向developer.android.com/guide/topics/manifest/…
  • 清单文件中活动的方向是“横向”模式。问题在于 RecyclerView GridLayoutManager 方向行为。
  • @Gaurav 你有没有用 RecyclerView & GridLayout Manager 找到任何潜在的解决方案?
  • @Gaurav 你找到解决方案了吗?

标签: android gridlayoutmanager


【解决方案1】:

试试

new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false);

这里 1 算作行数。如果您使用片段,请使用getActvity(),如果您在活动中调用它,请使用MainActivity.this

【讨论】:

  • Rainmaker,感谢您的回答,上面的代码将允许水平滚动,但我在布局中有很多行并垂直分配索引(按行)。我需要水平索引。
  • 也许我不明白你的意思,把你需要的行数(5,10)....你不能同时垂直和水平无休止地滚动
【解决方案2】:

您可以尝试将Recyclerview 放入HorizontalScrollingView 并禁用Recyclerview 的垂直滚动:

您的 .xml 文件可能如下所示:

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

    <HorizontalScrollView
        android:id="@+id/myHorizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

    </HorizontalScrollView >

</LinearLayout >

在您的代码中,您必须覆盖 canScrollVerticallyGridLayoutManager

Java:

GridLayoutManager mLayoutManager = new GridLayoutManager(this, 100) {
    public boolean canScrollVertically() {
        return false;
    }
};

科特林:

val mLayoutManager = object : GridLayoutManager(this, 100) {
    override fun canScrollVertically() : Boolean {
        return false
    }
}

这 100 行可能太多,无法一次在屏幕上显示。要解决这个问题,请不要禁用GridLayoutManager 中的垂直滚动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2014-01-09
    • 1970-01-01
    相关资源
    最近更新 更多