How to display a button on long press of a list it

2019-03-31 22:09发布

I need to display a delete button on long press of a list item..

I've got the code for long press.. but don't know how to code for displaying a button inside this long press...

3条回答
放荡不羁爱自由
2楼-- · 2019-03-31 22:40

You can use Alert dialog. Here is an example

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                final CharSequence[] items = { "Delete Item" };


                AlertDialog.Builder builder = new AlertDialog.Builder(
                        [CLASS_NAME].this);
                builder.setTitle("Delete Item");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        Intent i;
                        switch (item) {
                        case 0:
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    SelectProfile.this);
                            builder.setMessage(
                                    "Are you sure you want to delete?")
                                    .setCancelable(false)
                                    // Prevents user to use "back button"
                                    .setPositiveButton(
                                            "Delete",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {
                                                    //Todo code here
                                                }
                                            })
                                    .setNegativeButton(
                                            "Cancel",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {
                                                    dialog.cancel();
                                                }
                                            });
                            builder.show();
                            break;
                        }
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
                return false;
            }
        });
查看更多
放我归山
3楼-- · 2019-03-31 22:43

Finally got the answer...

.xml file

<ImageButton
        android:id="@+id/imgdelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete" 
        android:visibility="invisible"/>

.java file

      lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            arg1.findViewById(R.id.imgdelete).setVisibility(View.VISIBLE);
            return false;
        }
    });
}
查看更多
萌系小妹纸
4楼-- · 2019-03-31 22:48

First you have to make that delete button invisible using code, or setting it's property in xml file. When the user clicks on longpress you have to make that delete button visible. After the delete action is completed, make that button invisible again.

查看更多
登录 后发表回答