android - 试图刷掉 native FlatList 中的项目但没有收到 onResponderTerminationRequest 事件

标签 android ios react-native touch gestures

我正在使用 react-native 我有一个显示一系列通知的 FlatList。 我使用内置的 Gesture Responder System允许用户滑动他们不再感兴趣的通知。

我的列表项是这样工作的...

<View style={[
            props.style,
            this.state.is_swipeing?{opacity: 0.5}:{},
            (this.state.swipe_direction=="RIGHT")?{backgroundColor:"#CCCCCC"}:{},
        ]}
        onStartShouldSetResponder={(evt)=>{
            console.log('onStartShouldSetResponder');
            return true;
        }}
        onResponderTerminationRequest={(evt)=>{
            console.log('onResponderTerminationRequest');
            if (this.state.swipe_direction == "RIGHT" || this.state.swipe_direction == "LEFT") {
                console.log("dont let go!!!");
                return false;
            }
            console.log("give it up");
            return true;
        }}
        onResponderTerminate={(evt)=>{
            console.log('onResponderTerminate');
            this.setState({is_swipeing: false, start_x: null, start_y: null, swipe_direction: null });
        }}
        onResponderGrant={(evt)=>{
            console.log('onResponderGrant');
            this.setState({is_swipeing: true, start_x: evt.nativeEvent.locationX, start_y: evt.nativeEvent.locationY, swipe_direction: null });
        }}
        onResponderMove={(evt)=>{
            var dx = evt.nativeEvent.locationX - this.state.start_x;
            var dy = evt.nativeEvent.locationY - this.state.start_y;
            if (Math.abs(dx) > Math.abs(dy)) { // big delta X
                if (dx > 40) { // swiping right
                    if (this.state.swipe_direction != "RIGHT") {
                        console.log("Swipeing right");
                        this.setState({ swipe_direction: "RIGHT"});
                    }
                }
                else if (dx < -40) { //swiping left
                    if (this.state.swipe_direction != "LEFT") {
                        console.log("Swipeing left");
                        this.setState({ swipe_direction: "LEFT"});
                    }
                }
                else {
                    if (this.state.swipe_direction != null) {
                        console.log("Not swipeing");
                        this.setState({ swipe_direction: null});
                    }
                }
            }
            else if (Math.abs(dy) > Math.abs(dx)) { // big delta Y
                if (dy > 40) { // swiping down
                    if (this.state.swipe_direction != "DOWN") {
                        console.log("Swipeing down");
                        this.setState({ swipe_direction: "DOWN"});
                    }
                }
                else if (dy < -40) { //swiping up
                    if (this.state.swipe_direction != "UP") {
                        console.log("Swipeing up");
                        this.setState({ swipe_direction: "UP"});
                    }
                }
                else {
                    if (this.state.swipe_direction != null) {
                        console.log("Not swipeing");
                        this.setState({ swipe_direction: null});
                    }
                }
            }
        }}
        onResponderRelease={(evt)=>{
            switch (this.state.swipe_direction) {
                case "UP": {
                    if (props.onSwipeUp) {
                        props.onSwipeUp();
                    }
                    break;
                }
                case "DOWN": {
                    if (props.onSwipeDown) {
                        props.onSwipeDown();
                    }
                    break;
                }
                case "LEFT": {
                    if (props.onSwipeLeft) {
                        props.onSwipeLeft();
                    }
                    break;
                }
                case "RIGHT": {
                    if (props.onSwipeRight) {
                        props.onSwipeRight();
                    }
                    break;
                }
                default: {
                    if (props.onPress) {
                        props.onPress();
                    }
                    break;
                }
            }
            this.setState({is_swipeing: false, start_x: null, start_y: null, swipe_direction: null });
        }}
    >
        {props.children}
    </View>

这一切都很好,直到我的列表中有足够的项目使列表可以滚动。

一旦列表足够长可以滚动,当我向左或向右滑动时,如果我向上或向下移动,那么 FlatList 会窃取响应者跟踪,我会收到 onResponderTermination 事件。使用户几乎不可能滑动列表中的项目。

我希望(根据上面引用的文档)我应该得到一个 onResponderTerminationRequest 事件,询问我是否愿意让步。但该事件永远不会被触发。

我的问题是......我有什么方法可以让我按住那个滑动,以便我可以确定 delta Y 是否足够重要以在放弃控制之前开始滚动?

或者,是否有另一种方法可以解决这个问题,感觉就像一个不错的可滚动列表,但仍然允许向左/向右滑动而不会短路手势?

我确定这在 Android VM 和我可以使用的所有 Android 物理设备上都有问题。我还没有确认它是否也是 IOS 设备或模拟器上的问题。

最佳答案

有一个库叫做 https://github.com/gitboss2000/react-native-swipeable-flat-list

我希望它比您的组件更适合您。 它们的实现并没有那么复杂,也许您可​​以通过它来修复您的组件。

关于android - 试图刷掉 native FlatList 中的项目但没有收到 onResponderTerminationRequest 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48452132/

相关文章:

react-native - 我正在使用 react-native-swiper-flatlist ,但我不知道如何在按下按钮时使用 scrollToIndex

reactjs - 在 ListView React Native 中循环嵌套数组对象

ios - React Native IAP 自动续订订阅检查过期

使用 SimpleAdapter 的 Android ListActivity - 突出显示所选项目

android - 如何使用自定义权限向其他APP发送OrderedBroadcast?

ios - UITableView以级联动画的方式进行行删除和插入

ios - 所有 iPad 和 iPad Pro 的 View 之间的常规/常规间距?

android - 涟漪效应在 ListView 中不起作用

android.support.design.widget.CollapsingToolbarLayout 无法实例化

c++ - 将原始 IOS 相机数据上传到纹理