RecyclerView滑动而不删除项目

时间:2019-05-04 14:32:45

标签: java android kotlin android-recyclerview

我想对RecyclerView中的滑动进行一些操作。我为此使用ItemTouchHelper.SimpleCallback。

但是当我滑动时,一个项目从RecyclerView中消失了。

该如何解决?我想显示一些滑动动画并调用onSwiped()回调。但不要删除项目。

<form class="form-horizontal" method="POST" action="employeesdelete.php">
    <input type="hidden" id="empid" name="id">
    <a class="close" href="#">&times;</a>
    <center><h2>Do you want to delete?</h2><hr></center>
    <div>
        <center>
            <select name="optemployee" required id="soflow">
                <option value="" selected>- Select Employee -</option>
<?php

$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "dbamps";

//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword,$dbname);
$sql = "SELECT * FROM employees";
$query = $conn->query($sql);
while($prow = $query->fetch_assoc()){
    echo "<option value='".$prow['employee_id']."'>".$prow['firstname'].' '.$prow['lastname'].' - '.$prow['employee_id'];"</option>";
}
?>
            </select>
        </center><br>
    </div>
    <input type="submit" class="btn btn-primary btn-block btn-large" name="deleteemp" value="You're Fired!">
</form>             


<?php
    $conn=mysqli_connect("localhost","root");
    if(!$conn)
        die('Cannot connect to the server!'.mysql_error()); 

    mysqli_select_db($conn,"dbamps");


    $temp1= $_POST["optemployee"];

    $sql="DELETE FROM employees WHERE = '$temp1'";

    mysqli_query($conn,$sql);
    mysqli_close($conn);

    echo '<script type="text/javascript">'; 
    echo 'alert("Employee removed!");'; 
    echo 'window.location = "employees.php";';
    echo '</script>';
?>

1 个答案:

答案 0 :(得分:0)

您可以通过添加到 ViewHolder 字段来做到这一点,该字段将包含有关视图是否可滑动的信息,并在方法 onSwiped 上将其设置为 false,并使用方法 translationX(0) 将项目动画化为原始 x,您也可以使用 transaltionY( 0) 如果您也在垂直滑动。如果您这样做,该项目将无法再次滑动

   private ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        Log.e("Swiped","Swiped");
        ((YourViewHolder) viewHolder).setSwipeable(false);
        View item = viewHolder.itemView;
        item.animate().translationX(0);
    }

    @Override
    public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        if(((RemarkAdapter.RemarkViewHolder) viewHolder).isSwipeable()){
            return super.getSwipeDirs(recyclerView, viewHolder);
        }
        else{
            return 0;
        }
    }

};