我已經實現了用panResponder和ScrollView拖拽拖放列表。我希望即使在觸摸該項目時也能夠滾動列表。問題在於,當我做手勢滾動時,項目會移動。當然,我也希望能夠移動該項目,但現在它具有與滾動相同的手勢。我想通過只在長時間按下(1.5秒)後拖動元素來克服它。如何實現它?我想使用觸摸作爲onPressIn/onPressOut的元素,就像這裏所述:http://browniefed.com/blog/react-native-press-and-hold-button-actions/ 並以某種方式在時間段後啓用panResponder,但我不知道如何以編程方式啓用它。啓用長按後在滾動視圖中拖動元素
現在這是我在列表元素代碼:
class AccountItem extends Component {
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY(),
zIndex: 0,
}
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder:() => true,
onPanResponderGrant: (e, gestureState) => {
this.setState({ zIndex: 100 });
this.props.disableScroll();
},
onPanResponderMove: Animated.event([null, {
dx: this.state.pan.x,
dy: this.state.pan.y,
}]),
onPanResponderRelease: (e, gesture) => {
this.props.submitNewPositions();
Animated.spring(
this.state.pan,
{toValue:{ x:0, y:0 }}
).start();
this.setState({ zIndex: 0 });
this.props.enableScroll();
}
})
}
meassureMyComponent = (event) => {
const { setElementPosition } = this.props;
let posY = event.nativeEvent.layout.y;
setElementPosition(posY);
}
render() {
const {name, index, onChangeText, onRemoveAccount} = this.props;
return (
<Animated.View
style={[this.state.pan.getLayout(), styles.container, {zIndex: this.state.zIndex}]}
{...this.panResponder.panHandlers}
onLayout={this.meassureMyComponent}
>
some other components...
</Animated.View>
)
}
}
export default AccountItem;
對於Android,請務必在'PanResponder'上設置'onShouldBlockNativeResponder'返回'false',否則如果您觸摸PanResponder對象,滾動將無法正常工作。根據:https://facebook.github.io/react-native/docs/panresponder.html –