React native sticky tab吸頂功能
rn的吸頂實現思路和h5不同,且安卓可能有相容性問題
思路一
使用ScrollView的stickyHeaderIndices屬性
- IOS支援,Android不支援
- 吸頂目標需要是Scrollview的第n+1個子節點
<ScrollView
style={ s.container }
onScroll = { this.onScroll }
scrollEventThrottle = {1}
showsVerticalScrollIndicator = { false }
stickyHeaderIndices={[n]} // IOS標題吸頂
>
{ content }
</ScrllView>
思路二
使用Animated+插值運算
- iOS使用原生動畫,效能較好
- 安卓部分app不支援原生動畫,需設定useNativeDriver=false,此時會有相容問題
import { View, Animated, FlatList } from 'react-native'
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList) //使用flatlist需要用Animated包裹一下
state = {
scrollY: new Animated.Value(0)
}
render(){
const HEIGHT = 200
const translateY = this.state.scrollY.interpolate({ //插值運算
inputRange: [-1, 0, HEIGHT, HEIGHT+1],
outputRange: [0, 0, -HEIGHT, -HEIGHT],
})
return (
<Animated.View
style={[{ transform: [{ translateY: translateY }]}]}
>
{ headerContent }
{ stickyContent }
<Animated.ScrollView // 或<AnimatedFlatList />
scrollEventThrottle = {16}
showsVerticalScrollIndicator = {false}
onScroll={ Animated.event([{
nativeEvent: { contentOffset: { y: this.state.scrollY } }
}], {
useNativeDriver: isIOS, // 是否觸發原生動畫
/// listener: (e) => this._onScroll(e),
})
}
>
{ scrollContent }
</Animated.ScrollView>
</Animated.View>
)
}
- 官網介紹:https://reactnative.dev/blog/2017/02/14/using-native-driver-for-animated
- 使用該屬性後,觸發的原生動畫驅動,不會觸發render,達到優化效能的目的
- 官網介紹是支援android的,但是實際我在用的時候設useNativeDriver=true時安卓上沒有transform效果,設為false才有
待解決問題
由於安卓使用的useNativeDriver=false,使得在小米手機上表現不太好,滾動時stickytab會明顯抖動
可優化點:調整flatlist設定、固定上劃高度、改監聽頻率、調整插值運算、發包對比本地執行效果等等
相關文章
- 講講吸頂效果與react-stickyReact
- React Native 速成 005 — React Navigation 之 Tab NavigatorReact NativeNavigation
- 【Android】 banner+tab吸頂+viewpager切換+重新整理載入之實現AndroidViewpager
- jQuery 頁面滾動 吸頂 和 吸底jQuery
- 仿京東、淘寶首頁,通過兩層巢狀的RecyclerView實現tab的吸頂效果巢狀View
- react-native實現畫筆功能React
- Vue開發——實現吸頂效果Vue
- [React Native]react-native-scrollabReact Native
- 向React Native應用新增螢幕捕捉功能React Native
- jQuery實現吸頂動畫導航欄jQuery動畫
- React Native 使用 react-native-webview 渲染 HTMLReact NativeWebViewHTML
- React Native Icon方案:react-native-svgReact NativeSVG
- React Native轉web方案:react-native-webReact NativeWeb
- iOS React Native 混合開發整合React NativeiOSReact Native
- RecyclerView使用指南(五)—— 實現吸頂效果View
- react-native配置react-native-image-crop-pickerReact
- React Native 上手React Native
- react-nativeReact
- react native FlatListReact Native
- Hello React NativeReact Native
- 直播軟體搭建,利用原生JS實現回到頂部以及吸頂效果JS
- app直播原始碼,利用原生JS實現回到頂部以及吸頂效果APP原始碼JS
- React Native / React除錯技巧React Native除錯
- Flutter 頁面滾動吸頂詳解(NestedScrollView)FlutterView
- 《React Native高效開發》之create-react-native-appReact NativeAPP
- JavaScript tab選項卡功能JavaScript
- Tips-移動端滑動固頂效果(position: sticky)
- 高仿京東Android App,整合React-Native熱更功能AndroidAPPReact
- 從零開始構建React Native數字鍵盤功能React Native
- React Native選擇器元件-react-native-slidepickerReact Native元件IDE
- react native 包學不包會系列--認識react nativeReact Native
- react-native呼叫native步驟React
- react/react-native效能優化React優化
- Android RecyclerView實現頭部懸浮吸頂效果AndroidView
- 安卓當下最流行的吸頂效果的實現安卓
- 詳解 React NativeReact Native
- React Native 熱更新React Native
- Redux for react native 指南ReduxReact Native