java - Android 点击并按住删除项目

标签 java android android-recyclerview ontouchlistener

我正在尝试添加新功能,如果您点击并按住列表中的某个项目,则会弹出一个窗口,询问您是否确定要删除此特定 项目。这意味着如果我单击并按住列表中的第一项,则会弹出一个对话框,询问我是否要删除它。单击"is"将会执行此操作。

实现这一目标的最佳方法是什么?

为了显示文件,我当前使用 RecyclerViewCardView

代码和图片如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        android:transitionName="open_mediaplayer"
        app:cardCornerRadius="4dp"
        app:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"
                android:id="@+id/imageView"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_gravity="center_vertical">
                    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="15sp"
                        android:fontFamily="sans-serif-condensed"
                        android:textStyle="bold"
                        android:id="@+id/file_name_text"
                        android:text="Audio File_2244322"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:textSize="12sp"
                        android:fontFamily="sans-serif-condensed"
                        android:layout_marginTop="7dp"
                        android:id="@+id/file_length_text"
                        android:text="00:44"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/file_time_added"
                        android:textSize="12sp"
                        android:text="30-12-2019"/>

                </LinearLayout>
            
        </LinearLayout>
        
    </android.support.v7.widget.CardView>

</LinearLayout>

这就是它的样子

This is how it looks like

最佳答案

我会在 RecyclerView 中的项目上使用 View.OnLongClickListener。触发监听器时,显示一条警报,询问用户是否确实要删除该项目。如果是,请在必要的地方(至少在数据集中)删除该项目,并使用 adapter.notifyItemRemoved(indexOfDeletedElement)

更新 RecyclerView

关于java - Android 点击并按住删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59741311/

相关文章:

Android:用 GestureDetector 可以检测到双指双击吗?

android - RecyclerView with GridLayoutManager in RecyclerView with LinearLayoutManager

java - Android:带有重复项的 SortedList

java - 查找加起来等于给定字符串的所有子字符串组合

java - 好的软件工程与安全

java - 正则表达式提取字符串,为什么我的模式不起作用?

android - 如何使用mediacodec在android中使用软件编解码器

Android - 如何动态更改 Recyclerview 高度?

java - 支持快速第k大元素查找的队列数据结构

java - Hibernate 类的搜索查询