【问题标题】:How to use Nested Arraylist with recyclerView?如何将嵌套数组列表与 recyclerView 一起使用?
【发布时间】:2020-07-29 08:27:39
【问题描述】:

我想填充一个嵌套的 ArrayList。 下面是来自服务器的 JSON 响应。现在我希望 sectionName 作为 headerView 和 sectionItems 作为该标题的子视图。所以,这个布局看起来像分类项目。 我已经研究过同样的问题,但找不到任何简单有效的解决方案。我不想创建一个单独的展平列表,因为这会花费我时间。 现在,我怎样才能实现这种布局。 PS:我不想使用任何外部库。

"data": [
{
  "sectionName": "Fruits",
  "sectionItems": [
    {
      "id": "123",
      "name": "Apple",
      "image": "url",
      "price": "120",
      "quntity": "600gm"
    },
    {
      "id": "234",
      "name": "Orange",
      "image": "url",
      "price": "340",
      "quntity": "2kg"
    },
    {
      "id": "334",
      "name": "Banana",
      "image": "url",
      "price": "340",
      "quntity": "2kg"
    }
  ]
},
{
  "sectionName": "Paints",
  "sectionItems": [
    {
      "id": "345",
      "name": "Red",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    },
    {
      "id": "346",
      "name": "White",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    },
    {
      "id": "347",
      "name": "Purple",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    }
  ]
}

编辑:我已经实现了一个嵌套的回收器视图,但无法呈现所有子元素。嵌套列表中有 80 多个项目,但我只能看到 18 个。

【问题讨论】:

  • 扁平化成本真的值得解决这个简单的问题吗?
  • @MariuszBrona 不多,但我想找到一种替代方法。因为我想学习。

标签: android arraylist android-recyclerview nested nestedlayout


【解决方案1】:

要回答您的问题,我认为最好的办法是展平列表。还有其他可能性,但它们真的适合 RecyclerView 的设计吗?我不这么认为。前段时间我写了一篇博文来展示你可以多么容易地实现分段 RecyclerView:

https://brona.blog/2020/06/sectioned-recyclerview-in-three-steps/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多