scrollView中的scroll tab,内容是flatlist,有一个header,如何解决scroll conflict
the scroll tab in the scrollView, which content is flatlist and has a header, how to resolve scroll conflict
我遇到一个问题,在页面上有一个header,一个滚动标签,主要内容是两个FlatList。
我使用动画,让 header 在单个视图中并使用绝对布局。这个解决方案有一个问题,FlatList 不能滚动,它只滚动 scrollView。当数据增长时,性能很差。
t是解决代码:
<Animated.View style={{
width: "100%",
position: "absolute",
transform: [{
translateY: this.headerY
}],
backgroundColor:'red',
top: 50,
elevation: 0,
flex: 1,
zIndex: this.state.pullToRefresh ? -1 : 1,
}}>
<HeaderView />
</Animated.View>
<Animated.ScrollView
scrollEventThrottle={1}
bounces={false}
showsVerticalScrollIndicator={false}
style={{ zIndex: 0, height: DeviceUtils.deviceHeight, elevation: -1,
overflow: 'scroll' }}
contentContainerStyle={{ paddingTop: NAVBAR_HEIGHT }}
onScroll={
Animated.event(
[{ nativeEvent: { contentOffset: { y: this.scroll } } }],
{ useNativeDriver: true },
)
}
overScrollMode="never"
/>}
>
<Tabs
initialPage={0}
renderTabBar={(props) =>
<Animated.View
style={[{
transform: [{ translateY: tabY }],
zIndex: 1,
width: "100%",
}]}>
<ScrollableTab
{...props}
style={{ backgroundColor: '#e5e5e5'}}
underlineStyle={{
backgroundColor: "#2a82e4",
}}
>
</ScrollableTab>
</Animated.View>
}
>
<Tab heading={"one"}
<FlatList key={'one'}/>
</Tab>
<Tab heading={"two"}
<FlatList key={'two'}/>
</Tab>
</Tabs>
UI 使用 nativebase UI 库
最后,我把表头放在了FlatList 的ListHeader 中。并根据项目索引来
渲染标签,当索引为0时,渲染标签。
<FlatList
data={DATA}
renderItem={this.renderItem}
keyExtractor={item => item.id}
ListHeaderComponent={header}
extraData={selected}
/>
....
renderItem = (item,index) => {
if(index == 0) {
return this.renderTabs()
} else {
return <Cell data=item/>
}
}
...
renderTabs = () => {
<View>
<Tab/> // the view for tabs, we have to handle the tab change by ourself
<Tab/>
</View
}
I use two data array to save for different tabs.
我遇到一个问题,在页面上有一个header,一个滚动标签,主要内容是两个FlatList。 我使用动画,让 header 在单个视图中并使用绝对布局。这个解决方案有一个问题,FlatList 不能滚动,它只滚动 scrollView。当数据增长时,性能很差。
t是解决代码:
<Animated.View style={{
width: "100%",
position: "absolute",
transform: [{
translateY: this.headerY
}],
backgroundColor:'red',
top: 50,
elevation: 0,
flex: 1,
zIndex: this.state.pullToRefresh ? -1 : 1,
}}>
<HeaderView />
</Animated.View>
<Animated.ScrollView
scrollEventThrottle={1}
bounces={false}
showsVerticalScrollIndicator={false}
style={{ zIndex: 0, height: DeviceUtils.deviceHeight, elevation: -1,
overflow: 'scroll' }}
contentContainerStyle={{ paddingTop: NAVBAR_HEIGHT }}
onScroll={
Animated.event(
[{ nativeEvent: { contentOffset: { y: this.scroll } } }],
{ useNativeDriver: true },
)
}
overScrollMode="never"
/>}
>
<Tabs
initialPage={0}
renderTabBar={(props) =>
<Animated.View
style={[{
transform: [{ translateY: tabY }],
zIndex: 1,
width: "100%",
}]}>
<ScrollableTab
{...props}
style={{ backgroundColor: '#e5e5e5'}}
underlineStyle={{
backgroundColor: "#2a82e4",
}}
>
</ScrollableTab>
</Animated.View>
}
>
<Tab heading={"one"}
<FlatList key={'one'}/>
</Tab>
<Tab heading={"two"}
<FlatList key={'two'}/>
</Tab>
</Tabs>
UI 使用 nativebase UI 库
最后,我把表头放在了FlatList 的ListHeader 中。并根据项目索引来 渲染标签,当索引为0时,渲染标签。
<FlatList
data={DATA}
renderItem={this.renderItem}
keyExtractor={item => item.id}
ListHeaderComponent={header}
extraData={selected}
/>
....
renderItem = (item,index) => {
if(index == 0) {
return this.renderTabs()
} else {
return <Cell data=item/>
}
}
...
renderTabs = () => {
<View>
<Tab/> // the view for tabs, we have to handle the tab change by ourself
<Tab/>
</View
}
I use two data array to save for different tabs.