qui*_*nja 8 swipe ios react-native react-native-gesture-handler
我正在使用来自 React-Native-Gesture-Handler 的 Swipeable 来合并滑动以在我的页面上删除。当我按删除时,联系人会被删除,但可滑动保持打开状态。
我希望它在按下后关闭,但我似乎不知道该怎么做。
这是我的代码:
const RightActions = (progress, dragX) => {
return (
<TouchableOpacity onPress={()=>{DeleteContact(i)}}>
<View style={[ContactsStyles.rightAction]}>
<Text style={ContactsStyles.actionText}>Delete</Text>
</View>
</TouchableOpacity>
)
}
Run Code Online (Sandbox Code Playgroud)
这是我可以滑动的地方:
<Swipeable renderRightActions={RightActions} >
<View style={ContactsStyles.UserContainer}>
<Text numberOfLines={1} style={[Fonts.Name]}> {obj.firstname} {obj.lastname} </Text>
{/* Message/Call Container */}
<View style={ContactsStyles.ImageCont}>
{/* Message */}
<TouchableOpacity onPress={() => Communications.text(obj.phone, 'Hey ' + obj.firstname + ', im in need of a Ryde. Are you able to pick me up? This is my current location: ' + location)} >
<View style={ContactsStyles.ImageBox}>
<Image style={ContactsStyles.Image} source={require('../../assets/icons/message.png')} />
</View>
</TouchableOpacity>
{/* Call */}
<TouchableOpacity onPress = {() => Communications.phonecall( obj.phone , true)}>
<View style={ContactsStyles.ImageBox}>
<Image style={ContactsStyles.Image} source={require('../../assets/icons/phone.png')} />
</View>
</TouchableOpacity>
</View>
{/* End of Message/Call Container */}
</View>
</Swipeable>
Run Code Online (Sandbox Code Playgroud)
Toz*_*art 20
您需要使用具有 close 方法的引用。
首先定义一个ref
const swipeableRef = useRef(null);
Run Code Online (Sandbox Code Playgroud)
然后将其分配给Swipeable
<Swipeable
ref={swipeableRef}
renderLeftActions={renderLeftActions}
onSwipeableLeftOpen={() => handleComplete(item)}
>
...
</Swipeable>
Run Code Online (Sandbox Code Playgroud)
然后调用 close 方法
const closeSwipeable = () => {
swipeableRef.current.close();
}
Run Code Online (Sandbox Code Playgroud)
请注意,对于多个 Swipeables,您应该有多个 refs。
let row: Array<any> = [];
let prevOpenedRow;
renderItem ({ item, index }) {
return (
<Swipeable
ref={ref => row[index] = ref}
friction={2}
leftThreshold={80}
rightThreshold={40}
renderRightActions={renderRightActions}
containerStyle={style.swipeRowStyle}
onSwipeableOpen={closeRow(index)}
...
>
...
</Swipeable>);
}
closeRow(index) {
if (prevOpenedRow && prevOpenedRow !== row[index]) {
prevOpenedRow.close();
}
prevOpenedRow = row[index];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2594 次 |
| 最近记录: |