reactjs - 当标题在SectionList中变得粘滞时如何获取事件?

标签 reactjs react-native react-redux react-native-sectionlist

当标题粘贴在部分列表顶部时,我想更改标题的样式。所以我想要一些事件让我知道标题现在被粘住以便改变其样式。

              <SectionList
                sections={this.state.list}
                keyExtractor={(item, index) => item.id + index}
                renderItem={({ item, index }) =>
                  <CustomItem
                    title={item.name}
                    description={item.appDescription}
                  >
                  </ExpandableItem>}
                renderSectionHeader={({ section: { title, id } }) => (
                  this.state.list.length > 1 ? <View style={styles.sectionHeader} >
                    <Text style={styles.sectionHeaderTitle}>{title}</Text>
                  </View> : null
                )}
                ListHeaderComponent={() => {
                  return (<View style={{ paddingTop: 20 }} />);
                }}
            stickySectionHeadersEnabled={true}
            removeClippedSubviews={true}
              />```

最佳答案

使用动画部分列表

const AnimatedSectionList = Animated.createAnimatedComponent(SectionList);

然后捕获其偏移量,例如

const offset = new Animated.Value(0);

并在 onScroll 事件中使用它。

现在使用插值在偏移特定值更改标题样式 我在下面使用您的代码添加一个小示例,该代码通过滚动更改高度

<SectionList
            sections={this.state.list}
            keyExtractor={(item, index) => item.id + index}
onScroll={Animated.event(
        [
          {
            nativeEvent: { contentOffset: { y: offset } },
          },
        ],
        {
          useNativeDriver: true,
        }
      )}
            renderItem={({ item, index }) =>
              <CustomItem
                title={item.name}
                description={item.appDescription}
              >
              </ExpandableItem>}
            renderSectionHeader={({ section: { title, id } }) => (
              this.state.list.length > 1 ? <View style={styles.sectionHeader} >
                <Text style={styles.sectionHeaderTitle}>{title}</Text>
              </View> : null
            )}
            ListHeaderComponent={() => {
              return (<View style={{ paddingTop: 20, height: offset.interpolate({
        inputRange: [0, 30],
        outputRange: [30, 0],
      }) }} />);
            }}
        stickySectionHeadersEnabled={true}
        removeClippedSubviews={true}
          />

关于reactjs - 当标题在SectionList中变得粘滞时如何获取事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72686541/

相关文章:

javascript - 无法渲染简单的 ReactDOM div

reactjs - Redux-saga:dispatch 有时不会触发 saga

javascript - 尝试遍历 React 中的多个组件

javascript - 在多个元素上切换类 react

javascript - Reactjs 中是否尊重多个 setState() 的顺序?

reactjs - react-pdf中EOF后出现错误: stream. push()

javascript - 使用 crypto js AES ECB 算法在 JavaScript 中加密字节数组

android - React Native 中 Text 组件的 accessible、a​​ccessibilityLabel 和 accessibilityHint 属性有什么区别?

ios - React-Native 与现有应用程序集成 'RCTRootView.h' 找不到文件

javascript - 类型错误:无法读取未定义的 React 属性 'any'