我正在尝试添加新功能,如果您点击并按住列表中的某个项目,则会弹出一个窗口,询问您是否确定要删除此特定 项目。这意味着如果我单击并按住列表中的第一项,则会弹出一个对话框,询问我是否要删除它。单击"is"将会执行此操作。
实现这一目标的最佳方法是什么?
为了显示文件,我当前使用 RecyclerView 和 CardView。
代码和图片如下:
<?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>
这就是它的样子
最佳答案
我会在 RecyclerView 中的项目上使用 View.OnLongClickListener
。触发监听器时,显示一条警报,询问用户是否确实要删除该项目。如果是,请在必要的地方(至少在数据集中)删除该项目,并使用 adapter.notifyItemRemoved(indexOfDeletedElement)
关于java - Android 点击并按住删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59741311/