【问题标题】:GridLayoutManager - column width wrap its own largest childGridLayoutManager - 列宽换行它自己最大的孩子
【发布时间】:2016-12-14 16:21:27
【问题描述】:

我在HorizontalScrollView 中有一个RecyclerView,我希望它使用GridLayoutManager。这没关系,但有一件事仍然困扰着我,每列的宽度都是相同的(基于我想的最大单元格宽度?)。是否可以将列的宽度换行以匹配该特定列的最大单元格?

它应该看起来像这样:

橙色部分是单元格视图所占据的部分。


编辑

我们要求我澄清我的期望。举个例子胜过千言万语,这里你可以看到一个带有 GridLayoutManager 的 RecyclerView 的截图。每个项目都是一个简单的 TextView,其中随机包含 10 到 40 个字符之间的文本。如前所述,RecyclerView 位于 Horizo​​ntalScrollView 内。我们可以看到每一列都有相同的宽度,尽管这一列中的任何项目都不能满足整个宽度。我想要的是删除那些无用的空白空间并拥有不同大小的列,每列与其最大的孩子的宽度相匹配。

如果你想测试这种行为,你可以克隆我在 Github 上上传的这个 repo:https://github.com/ShargotthDev/TestGrid

按照要求,这是我的 XML 布局(非常基本):

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:id="@+id/gameplay_hotizontalScroll_ScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="70dp">

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

    </HorizontalScrollView>

</RelativeLayout>

编辑 2

我应该提到一些单元格的跨度大小可能 > 1 并且 LayoutManager 应该是垂直的,以便这些单元格水平而不是垂直占据更多位置(不知道我是否让自己可以理解)。

感谢您的宝贵时间!

【问题讨论】:

  • 还是不明白你到底想要什么??
  • 你能发布你的xml布局吗?你用过columnSpancolumnWeight吗?
  • @HugoGresse 我发布了我的 XML 布局,但不,我不使用 columnSpan 或 columnWeight,但这不是必需的,因为我希望每列都有不同的宽度。还是我错了?
  • 对我来说你应该有一个预定义的网格。正如我们为 web 所做的那样,一个页面被分成 12 个“单元”或网格。然后你在这个网格上对齐你的元素。所以你可以削减你的布局并使用 columnWeight。让我知道它是否解决了问题。你在文档中找到什么了吗?

标签: android android-recyclerview gridlayoutmanager android-wrap-content


【解决方案1】:

您不需要将 RecyclerView 放在 Horizo​​ntalScrollView 中。请参阅下面的代码。

public class MainActivity extends AppCompatActivity {

    String[] list = new String[]{"Some text goes here", "Some small", "text", "goes here", "Some", "very large text", "goes here",
            "Some text goes here", "Some small", "text", "goes here", "Some", "very large text", "goes here"};
    RecyclerView grid;
    GridAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grid = (RecyclerView)findViewById(R.id.grid);
        grid.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.HORIZONTAL, false));
        grid.setHasFixedSize(true);
        adapter = new GridAdapter(list);
        grid.setAdapter(adapter);
    }
}

适配器类

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder>{
    String[] mList;
    public GridAdapter(String[] list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(mList[position]);
    }

    @Override
    public int getItemCount() {
        return mList.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.text);
        }

        public void bind(String s) {
            textView.setText(s);
        }
    }
}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="10dp">
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cab.suresh.gridlayoutexample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

编辑 像这样将你的 RecyclerView 放在 NestedScrollView 中

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>

并像这样设置您的 spanCount 数量

spanCount = 8;

grid.setLayoutManager(new GridLayoutManager(this, spanCount, LinearLayoutManager.HORIZONTAL, false));

【讨论】:

  • 这真的很好,它实际上回答了这个问题。但我应该更精确,我使用此 RecyclerView/ScrollView 组合生成的 2D 表可能包含 spanSize > 1 的单元格,但是因为您的解决方案需要水平设置 LayoutManager,所以 spanSize > 1 的单元格不会看起来与垂直 LayoutManager 一样。 (加上我需要重新组织数据集的创建方式这一事实,但这并非不可能)。 PS:如果它真的解决了我的问题,我不会不接受答案,别担心。
  • 如果我没有找到任何解决方案,我会接受你的解决方案,因为它解决了最初描述的问题。
  • 无法获得您期望的结果。你能清楚地解释你的期望吗?
  • 我会尽量说清楚。我想要的是生成一个叫做“统计表”的东西。这些表可能有任意数量的行和任意数量的列。我首先使用 TableLayout 处理它,它适用于小桌子,但是当涉及到有很多行/列的表格时,它会花费太多时间来膨胀。这就是我开始使用 RecyclerView 生成表的原因。此链接上提供了“统计表”的外观示例:img11.hostingpics.net/pics/…。我的期望是使用 RV 创建这种表。
  • 如果我使用 ScrollView/RV 组合是因为行数和列数可能真的很多(例如 500 行和 15 列),因此表格应该可以双向滚动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2019-09-12
相关资源
最近更新 更多