当我滚动时,react-native SectionList不能scrollToLocation

时间:2019-08-27 07:27:56

标签: react-native react-native-android react-native-sectionlist

我想实现一个类似于通讯录的组件,但是如果没有滚动条,单击右边的字母时,SectionList可以正常跳转。但是当它滚动时(惯性还没有结束),单击正确的字母不会跳动

反应性:0.58.6 反应:16.8.3

 <SectionList
                ref="sectionList"
                renderSectionHeader={this.renderSectionHeader}
                getItemLayout={this.itemLayout}
                sections={this.props.dataList}
                renderItem={this.renderItem}
                keyExtractor={(item, index)=> String(index)}
                onScrollBeginDrag={(event)=>{
                    this.refs.letterspace.changeActive()
                }}
                refreshing={this.props.isRefreshing}
            />

点击右字母会触发以下功能

getIndex = (index) => {
        let rollIndex = index
        // console.log(rollIndex,'rollindex')
        this.refs.sectionList.scrollToLocation({animated: false, itemIndex: 0, sectionIndex: rollIndex})
    }

1 个答案:

答案 0 :(得分:0)

您可能需要 forwardRef。下面是一个提供节列表的组件示例。请注意,最终导出的是带有 forwardRef

的导出
import React, { ForwardedRef, forwardRef, PropsWithRef } from 'react';
import { SectionList, SectionListData, SectionListProps } from 'react-native';

export type SectionScrollViewProps = PropsWithRef<
  Omit<SectionListProps<any>, 'sections' | 'renderItem'> & {
    children: JSX.Element[];
  }
>;
function SectionScrollViewWithRef(
  { children, ...props }: SectionScrollViewProps,
  ref: ForwardedRef<SectionList<JSX.Element>>
): JSX.Element {
  function renderItem({ item }: { item: JSX.Element }): JSX.Element {
    return item;
  }
  const sectionListSections: SectionListData<JSX.Element>[] = children.map((child) => ({
    data: [child],
  }));
  return (
    <SectionList ref={ref} sections={sectionListSections} renderItem={renderItem} {...props} />
  );
}
export const SectionScrollView = forwardRef(SectionScrollViewWithRef);

这是一个如何使用它的例子

import { useTheme } from "@react-navigation/native";
import React, { createRef } from "react";
import { Button, SectionList, Text, View } from "react-native";
import { SectionScrollView } from "./SectionScrollView";

export function ScrollSectionViewScreen() {
  const { colors } = useTheme();
  const scrollViewRef = createRef<SectionList>();
  return (
    <View style={{ flex: 1 }}>
      <Button
        color={colors.primary}
        onPress={() => {
          scrollViewRef.current?.scrollToLocation({
            sectionIndex: 0,
            itemIndex: 0,
          });
        }}
        title="0"
      />
      <Button
        color={colors.primary}
        onPress={() => {
          scrollViewRef.current?.scrollToLocation({
            sectionIndex: 1,
            itemIndex: 0,
          });
        }}
        title="1"
      />
      <Button
        color={colors.primary}
        onPress={() => {
          scrollViewRef.current?.scrollToLocation({
            sectionIndex: 5,
            itemIndex: 0,
          });
        }}
        title="5"
      />
      <SectionScrollView
        ref={scrollViewRef}
        style={{ flex: 1, borderColor: "yellow", borderWidth: 10 }}
      >
        <View
          key="0"
          style={{
            borderColor: colors.border,
            borderWidth: 10,
            minHeight: 300,
          }}
        >
          <Text style={{ color: colors.text }}>Section 0</Text>
        </View>
        <View
          key="1"
          style={{
            borderColor: colors.border,
            borderWidth: 10,
            minHeight: 300,
          }}
        >
          <Text style={{ color: colors.text }}>Section 1</Text>
        </View>
        <View
          key="2"
          style={{
            borderColor: colors.border,
            borderWidth: 10,
            minHeight: 300,
          }}
        >
          <Text style={{ color: colors.text }}>Section 2</Text>
        </View>
        <Text key="2" style={{ color: colors.text }}>
          Section 2
        </Text>
        <Text key="3" style={{ color: colors.text }}>
          Section 3
        </Text>
        <Text key="4" style={{ color: colors.text }}>
          Section 4
        </Text>
        <View
          key="5"
          style={{
            borderColor: colors.border,
            borderWidth: 10,
            minHeight: 600,
          }}
        >
          <Text style={{ color: colors.text }}>Section 5</Text>
        </View>
        <View
          key="6"
          style={{
            borderColor: colors.border,
            borderWidth: 10,
            minHeight: 600,
          }}
        >
          <Text style={{ color: colors.text }}>Section 6</Text>
        </View>
      </SectionScrollView>
    </View>
  );
}