android - 如何在 React Native 中实现类似 CoordinatorLayout 的功能?

标签 android react-native native

我想实现类似 android 的 CoordinatorLayout enter always for my ToolBar 我尝试了太多的解决方案,有些确实有效但不完全像 https://github.com/maolion/mao-rn-android-kit这真的很酷,但有一个挫折,因为它不适用于 ListView 我也尝试过 Animated,但 android 上的滚动事件限制在大多数时候都不起作用,它甚至不起作用。

使用 mao-rn-android-kit

<CoordinatorLayoutAndroid ref={(component) => this.coordinatorLayout = component} fitsSystemWindows={false}>
    <AppBarLayoutAndroid
        layoutParams={{
                            width: 'match_parent',
                            height: 112
                        }}
        style={{ backgroundColor:"#528eff" }}>
        <View layoutParams={{height: 56, width: this.windowWidth, scrollFlags: (
                                    AppBarLayoutAndroid.SCROLL_FLAG_SCROLL |
                                    AppBarLayoutAndroid.SCROLL_FLAG_ENTRY_ALWAYS)}} style={{height: 56}}>
            <ToolbarAndroid
                titleColor={'#FFF'}
                title={this.props.title}
                navIcon={images.arrowBack}
                onIconClicked={() => this._goBack()}
                onActionSelected={() => {}}
                actions={[{title: 'Search', icon: images.search, show: 'always'}]}
                style={[{backgroundColor: '#528eff', width: this.windowWidth, height: 56}]}/>
        </View>
        <View layoutParams={{height: 56, width: this.windowWidth}}
              style={{flex: 0, flexDirection: 'row', justifyContent: 'center', width: this.windowWidth, backgroundColor: '#528eff'}}>
            <TouchableOpacity onPress={() => this.getDocuments('high')}
                              style={[styles.highNormalLowDocListHeaderStateTextContainer, highSelected.borderStyle]}>
                <Text
                    style={[styles.highNormalLowDocListHeaderStateText, highSelected.textStyle]}>HIGH</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={() => this.getDocuments('normal')}
                              style={[styles.highNormalLowDocListHeaderStateTextContainer, normalSelected.borderStyle]}>
                <Text
                    style={[styles.highNormalLowDocListHeaderStateText, normalSelected.textStyle]}>NORMAL</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={() => this.getDocuments('low')}
                              style={[styles.highNormalLowDocListHeaderStateTextContainer, lowSelected.borderStyle]}>
                <Text
                    style={[styles.highNormalLowDocListHeaderStateText, lowSelected.textStyle]}>LOW</Text>
            </TouchableOpacity>
        </View>
    </AppBarLayoutAndroid>
    <View
        ref={(component) => this.contentLayout = component}
        style={{flex: 1, backgroundColor: 'transparent', height: this.windowHeight - 150}}>
        <ListView
            style={{height: this.windowHeight - 150, overflow: 'hidden'}}
            dataSource={this.state.documents}
            enableEmptySections={true}
            renderRow={(rowData) => this._renderRow(rowData)}
        />
    </View>
</CoordinatorLayoutAndroid>

最佳答案

流畅地响应滚动位置的关键是使用 Animated.eventonScroll 来更新 Animated.value

getInitialState: function() {
    return {
        scrollY: new Animated.Value(0)
    }
},
render() {
    return (
         <ScrollView
             scrollEventThrottle={16}
             onScroll={Animated.event(
                            [{nativeEvent: 
                               {contentOffset: 
                                 {y: this.state.scrollY}
                               }
                            }]
              )}>
    // etc ...
}

然后我可以在 this.state.scrollY 上调用 interpolate 并将这个值提供给另一个我想要的组件上的 transform 样式在 ScrollView 滚动时更新。

关于android - 如何在 React Native 中实现类似 CoordinatorLayout 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194181/

相关文章:

android - 我删除了 appcompat_v7,现在我的 Android 项目无法运行

java - 在 React Native 中安装 Native 模块后应用程序无法构建?

Android NDK 原生函数调用问题

java - 从 JNI 中的 native 代码退出会杀死 Java 应用程序

android - 最适合我的设备的 OEM 驱动程序?

android - 如何将 Canvas 置于其他 View 之上

javascript - 在 render() 中渲染单独的组件

java - 在 Android 上的所有应用中添加 KeyListener 和 Draw To Screen

java - 在 ListView 中从 URL 加载图像

react-native - 对拍照时 native 视觉相机卡住使用react