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

大憨熊 提交于 2019-12-05 03:10:27

问题


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...


回答1:


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;
        }
    });
}



回答2:


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.




回答3:


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;
            }
        });


来源:https://stackoverflow.com/questions/12136520/how-to-display-a-button-on-long-press-of-a-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!