user113428
user113428

Reputation: 61

How to Delete Item by click in Item for long time in Recycleview?

How to delete item by click in item of Recycleview for a long time (maybe 2 seconds) ? And when click in item for long time it give dialog : "Are you Sure To Delete?".

Upvotes: 0

Views: 911

Answers (2)

Jaimil Patel
Jaimil Patel

Reputation: 1347

You can add below code in onBindViewHolder method..

 holder.itemView.setOnLongClickListener {
val builder = AlertDialog.Builder(this)
        builder.setTitle("Delete")
        builder.setMessage("Are you Sure To Delete?")

        builder.setPositiveButton(android.R.string.yes) { dialog, which ->
            Toast.makeText(applicationContext,
                    android.R.string.yes, Toast.LENGTH_SHORT).show()
        }

        builder.setNegativeButton(android.R.string.no) { dialog, which ->
            Toast.makeText(applicationContext,
                    android.R.string.no, Toast.LENGTH_SHORT).show()
        }

        builder.setNeutralButton("Maybe") { dialog, which ->
            Toast.makeText(applicationContext,
                    "Maybe", Toast.LENGTH_SHORT).show()
        }
        builder.show()
        false
        }

Upvotes: 1

Destroyer
Destroyer

Reputation: 795

Adapter class

    OnLongItemClickListener longClickListener;

    @Override
    public void onBindViewHolder(final DocumentViewHolder viewHolder, int position) {

        ...
        viewHolder.itemView.setLongClickable(true);
        ...
    }

    public void setLongClickListener(OnLongItemClickListener longClickListener) {
       this.longClickListener = longClickListener;
    }

OnLongItemClickListener

interface OnLongItemClickListener{
 void longClick(Item item)
}

After in your activity / fragment implements OnLongClickListener after call adapter.setLongClickListener (this) in the method that appears, call alertDialog, when you click the ok button, remove item from the list and call

adapter.notifydatasetchanged()

Upvotes: 1

Related Questions