简体   繁体   English

试图刷掉react-native FlatList中的项目但不获取onResponderTerminationRequest事件

[英]Trying to swipe away items in react-native FlatList but not getting onResponderTerminationRequest events

I'm using react-native I have a FlatList that displays a sequence of notifications. 我正在使用react-native我有一个显示一系列通知的FlatList。 I use the built-in Gesture Responder System to allow the user to swipe away notifications they are no longer interested in. 我使用内置的手势响应系统允许用户刷掉他们不再感兴趣的通知。

My list items work like so... 我的清单项目就是这样......

<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>

This all works great, until I have enough items in my list that the list becomes scrollable. 这一切都很有效,直到我的列表中有足够的项目列表变得可滚动。

Once the list is long enough to be scrollable, as I swipe left or right, if I move up or down at all, then the FlatList steals the responder tracking, and I get the onResponderTermination event. 一旦列表足够长以便可滚动,当我向左或向右滑动时,如果我向上或向下移动,则FlatList窃取响应者跟踪,并且我得到onResponderTermination事件。 Making it almost impossible for a user to swipe away an item in the list. 使用户几乎不可能轻扫列表中的项目。

I would expect (based on the documentation referenced above) that I should be getting a onResponderTerminationRequest event, asking if I'm willing to yield the touch. 我希望(基于上面引用的文档)我应该得到一个onResponderTerminationRequest事件,询问我是否愿意接触。 But that event never gets fired. 但那件事永远不会被解雇。

My question is ... is there any way for me to hold on to that swipe so that I can determine whether or not the delta Y was significant enough to start scrolling before yielding control? 我的问题是......有没有什么方法可以让我坚持刷卡,以便我可以确定delta Y是否足以在产生控制之前开始滚动?

Or, is there another way to approach this that feels like a nice scrollable list, but still allows swiping left/right without short circuiting gestures? 或者,有没有另一种方法来处理这个感觉像一个很好的可滚动列表,但仍然允许左/右滑动没有短路手势?

I'm sure this has issues on android VMs, and all android physical devices I have at my disposal. 我确定这有关于Android VM的问题,以及我所拥有的所有Android物理设备。 I haven't confirmed if it is also a problem on IOS devices, or simulators. 我还没有确认它是否也是IOS设备或模拟器上的问题。

There is a library called https://github.com/gitboss2000/react-native-swipeable-flat-list 有一个名为https://github.com/gitboss2000/react-native-swipeable-flat-list的库

i hope it could work better for you, than your component. 我希望它比你的组件更适合你。 their implementation not so complicated maybe you can fix your component by that. 他们的实现不是那么复杂,也许你可以通过它来修复你的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM