React Native Swipeable(滑动删除)未关闭

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)


def*_*led 8

这是一个索引问题。当我们将列表键入index,并删除一项(例如索引=3)时,下一项(以前的索引=4)现在采用较早的索引。Swipeable 对此感到困惑。

答案是将列表指定为索引以外的其他内容(例如 unique id)。