1.1.5 • Published 8 months ago

react-native-waterfall-flow v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

特性

  • 性能方面表现突出,渲染速度快,滚动体验良好
  • 无需手动设置item高度,一切计算工作由组件内部完成
  • 属性和方法与FlatList完全一致,易于上手

Changelogs

展示案例

下面展示的是一个包括下拉刷新,上拉加载更多的高性能瀑布流列表

截屏

npm.io

截图gif

npm.io

安装

v1.1.5

npm install react-native-waterfall-flow --save

基本使用

import WaterfallFlow from 'react-native-waterfall-flow'
<WaterfallFlow
  data={data}
  numColumns={2}
  renderItem={({ item, index, columnIndex }) => {
    return (
      <View>
        <Text>index: {index}</Text>
        <Text>columnIndex: {columnIndex}</Text>
      <View/>
    )
  }}
  ...
/>

属性

几乎所有FlatList的属性都是支持的,这里只列出一些常用的属性,其余属性可查看FlatList文档

renderItem

TypeRequiredDescription
functiontrue用于将当前item渲染到列表中
  • item (Object): 当前项的数据
  • index (number): 当前项的索引
  • columnIndex (number): 当前项在列表中的第几列

示例用法:

<WaterfallFlow
  renderItem={({ item, index, columnIndex }) => {
    return (
      <View>
        <Text>index: {index}</Text>
        <Text>columnIndex: {columnIndex}</Text>
      <View/>
    )
  }}
  ...
/>

如何设置间距

renderItem方法暴露出了columnIndex,该属性表示当前item在列表中第几列,这样你就能够自由的控制每个item的上下左右间距了。item的宽度等于 瀑布流容器的宽度 / numColumns

示例用法:

<WaterfallFlow
  renderItem = {({ item, index, columnIndex }) => {
    return (
      <View
        style={{
          paddingLeft: columnIndex === 0 ? 12 : 6,
          paddingRight: columnIndex === 0 ? 6 : 12,
          paddingTop: 3,
          paddingBottom: 3
        }}
      >
      <View/>
    )
  }}
  ...
/>

data

TypeRequiredDescription
arraytrue瀑布流数据源,可以是任意内容的数组

numColumns

TypeRequiredDescription
numberfalse瀑布流的列数,默认为2,即两列

ListHeaderComponent

TypeRequiredDescription
component, functionfalse头部组件。可以是 React Component 或 render 函数。

示例用法:

<WaterfallFlow
  ListHeaderComponent = {
    <View>
      <Text>this is header<Text>
    <View/>
  }
  ...
/>

ListFooterComponent

TypeRequiredDescription
component, functionfalse尾部组件。可以是 React Component 或 render 函数。

示例用法:

<WaterfallFlow
  ListFooterComponent = {
    <View>
      <Text>this is footer<Text>
    <View/>
  }
  ...
/>

ListEmptyComponent

TypeRequiredDescription
component, functionfalse列表为空时渲染该组件。可以是 React Component 或 render 函数

示例用法:

<WaterfallFlow
  ListEmptyComponent = {
    <View>
      <Text>no data here<Text>
    <View/>
  }
  ...
/>

onEndReached

(info: {distanceFromEnd: number}) => void
TypeRequiredDescription
functionfalse当列表滚动到底部是调用

onRefresh

() => void
TypeRequiredDescription
functionfalse如果设置了此选项,则会在列表头部添加一个标准的RefreshControl 控件,以便实现“下拉刷新”的功能。同时你需要正确设置refreshing属性。

refreshing

TypeRequiredDescription
booleanfalse在等待加载新数据时将此属性设为 true,列表就会显示出一个正在加载的符号。

style

TypeRequiredDescription
view stylesfalse用于设置瀑布流外层样式,默认会有{ flex: 1 }的样式,即高度充满父容器

contentContainerStyle

TypeRequiredDescription
view stylesfalse瀑布流内容容器样式

建议将其用于设置容器背景色。 示例:

<WaterfallFlow
  contentContainerStyle={{ backgroundColor: '#f9f9f9' }}
  ...
/>

方法

所有和方法和FlatList保持一致,这里只列出几个常用的,其余方法可查看FlatList文档

scrollToEnd()

scrollToEnd([params])

滚动到瀑布流列表的底部

参数:
PropTypeRequired
paramsobjectfalse

params的参数如下:

  • 'animated' (boolean) - 是否有滚动动画. 默认 true.

方法调用示例

绑定ref:

<WaterfallFlow
  ref={ref => this.listRef = ref}
  ...
/>

调用方法:

this.listRef.scrollToEnd()

scrollToIndex()

scrollToIndex([params])

将位于指定位置的元素滚动到可视区的指定位置,当viewPosition 为 0 时将它滚动到屏幕顶部,为 1 时将它滚动到屏幕底部,为 0.5 时将它滚动到屏幕中央。

参数:
PropTypeRequired
paramsobjecttrue

params的参数如下:

  • 'animated' (boolean) - 是否有滚动动画. 默认 true
  • 'index' (number) - 滚动到指定元素的下标。 必须设置该属性
  • 'viewOffset' (number) - 偏移最终目标位置的固定像素数
  • 'viewPosition' (number) - 为 0 时将它滚动到屏幕顶部,为 1 时将它滚动到屏幕底部,为 0.5 时将它滚动到屏幕中央

scrollToOffset()

scrollToOffset([params])

滚动列表到指定的偏移(以像素为单位),等同于ScrollView的scrollTo方法。

参数:
PropTypeRequired
paramsobjecttrue

params的参数如下:

  • 'animated' (boolean) - 是否有滚动动画. 默认 true
  • 'offset' (number) - 要滚动到的偏移量。必须设置该属性

示例

示例代码 是一个expo app, 像这样启动项目

cd examples
npm i
npm start

License

react-native-waterfall-flow is MIT licensed, as found in the LICENSE file.

1.1.5

8 months ago

1.1.4

1 year ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago