【问题标题】:React Native FlatList - Variable columnsReact Native FlatList - 可变列
【发布时间】:2017-10-02 22:10:04
【问题描述】:

我正在开发一个无限滚动的产品列表,其中包含不同类型的产品。产品可以有特色也可以没有特色。当产品被推荐时,我们的产品卡片设计会占据手机的整个宽度,否则设计需要 2 列行

数据看起来像这样:

[
  {
    "type": "featured-product",
    "name": "Product",
    "description: "yadda yadda"
  },
  {
    "type": "product",
    "name": "Another Product",
    "description: "yadda yadda"
  },
  {
    "type": "product",
    "name": "And Another Product",
    "description: "yadda yadda"
  },
  {
    "type": "product",
    "name": "One more Product",
    "description: "yadda yadda"
  }
  {
    "type": "product",
    "name": "Yet another",
    "description: "yadda yadda"
  },
  {
    "type": "featured-product",
    "name": "This one's Featured though",
    "description: "yadda yadda"
  }
]

例如,列表如下所示:

--------------
|            |
|  Featured  |
|            |
--------------
|     ||     |
| Not || Not |
|     ||     |
--------------
|     ||     |
| Not || Not |
|     ||     |
--------------
|            |
|  Featured  |
|            |
--------------
|     ||     |
| Not || Not |
|     ||     |
--------------
|     ||     |
| Not || Not |
|     ||     |
--------------

但是,使用FlatList 组件,我没有运气实现这个设计的布局。

  • 为每个项目添加样式 flex: 1 全宽和 flex: 0.5 双列不允许换行并始终显示单列行
  • 将 numColumns 属性设置为 2 可以稍微正确地显示产品,但会导致全宽特色项目出现问题。
  • 考虑过将项目按 2 分块并让它们代表一个项目,但事实证明在使用 Flow 时需要更多开销

我一直在冒险使用 FlatList 以获得性能优势,但我开始认为它可能不适合我需要的布局。

以前有人解决过这个问题吗?

【问题讨论】:

  • 你能发布你的 renderer 代码来获取 FlatList 数据吗?
  • 如果您向我们提供呈现的标记,我们或许可以提出解决方案。
  • react-native-super-grid 以及@bennygenel 的回答可能对您有用。

标签: react-native flexbox react-native-android react-native-ios


【解决方案1】:

据我所知,FlatList 将数据中的每个项目呈现在其自己的行中。除非您操纵本机端以创建所需的行为组件,否则您所要求的所需行为是不可能的。

我认为您最好/最简单的方法是第三种选择。你能做什么我认为以某种方式操纵你正在呈现的数据并将所有其他“非特色”项目分成两部分并将它们一起呈现。

举例

renderItem({item}) {
  if (item.notFeatured === true) return <NotFeaturedRow data={item.items} />
  else return <FeaturedRow data={item} />
}

const data = [
  {
    "type": "featured-product",
    "name": "Product",
    "description: "yadda yadda"
  },
  {
    notFeatured: true, 
    items: [
      {
        "type": "product",
        "name": "Another Product",
        "description: "yadda yadda"
     },
     {
        "type": "product",
        "name": "And Another Product",
        "description: "yadda yadda"
      }    
    ]
  },
  {
    notFeatured: true, 
    items: [
      {
        "type": "product",
        "name": "Another Product",
        "description: "yadda yadda"
     },
     {
        "type": "product",
        "name": "And Another Product",
        "description: "yadda yadda"
      }    
    ]
  },
  {
    "type": "featured-product",
    "name": "This one's Featured though",
    "description: "yadda yadda"
  }
]

【讨论】:

  • 感谢您的洞察力!我最终选择了第三种选择。但是我使用了不同的数据结构,它仍然可以正常工作。
  • @Seth mine 只是一个例子来展示我的想法。我很高兴它对你有用。欢迎您
【解决方案2】:

只需使用 RecyclerListView 代替。它比 FlatList 更快,并在 Flipkart 进行了实战测试。它支持交错布局。

链接:https://github.com/Flipkart/ReactEssentials

您可以在此处阅读更多信息:https://medium.com/@naqvitalha/recyclerlistview-high-performance-listview-for-react-native-and-web-e368d6f0d7ef

【讨论】:

  • 它是否适用于 expo 或我将不得不退出应用程序?
猜你喜欢
  • 2018-04-05
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 2018-12-26
  • 2018-02-05
相关资源
最近更新 更多