получить значение Y компонента заголовка sectionlist - PullRequest
0 голосов
/ 25 июня 2019

Я создаю шпионскую прокрутку для списка разделов и хочу получить значения Y заголовка списка разделов,

Я пытался использовать метод onLayout, но получаю только значение y, равное 0. Я предполагаю, что мой компонент находится в собственном контейнере, поэтому мое представление получает 0 для этого контейнера.

это мой код:

<Animated.SectionList
                    initialNumToRender={5}
                    keyExtractor={(item) => item.docRef}
                    sections={data.slice()}
                    renderSectionHeader={this.renderSectionHeader}
                    renderItem={this.renderItem}
                    stickySectionHeadersEnabled={false}
                />

renderSectionHeader = ({section}) => {
    const index = section.sort;
    return (
        <View onLayout={e => this.onSectionLayout(e, index)}>
            <SectionListHeader section={section} index={index}/>
        </View>
    )
};

onSectionLayout = ({nativeEvent: {layout: {x, y, width, height}}}, index) => 
{
    console.log(height)
};

1 Ответ

1 голос
/ 25 июня 2019

это потому, что SectionHeader обернут родительским представлением, поэтому позиция y всегда равна 0.

альтернативный способ - ref.measure。like

constructor(props) {
    super(props)
    this.sectionHeaders = []
}

renderSectionHeader = ({section}) => {
    const index = section.sort;
    return (
        <View ref={c => this.sectionHeaders[index] = c}>
            <SectionListHeader section={section} index={index}/>
        </View>
    )
};

componentsDidMount() {
    setTimeout(() => {
      this.sectionHeaders.forEach((ref,index)=>{
        ref.measure((fx,fy,w,h,px,py) => {
           console.log('y pos is ',py))
        })
     })
  }, 500)
}
...