listview长按删除

listview长按删除

           listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                linearLayout=findViewById(R.id.linearLayout);
                checkBox=findViewById(R.id.cb_select);
                //显示checkbox
                checkBox.setVisibility(View.VISIBLE);
                linearLayout.setVisibility(View.VISIBLE);
                Button btn_cancle=findViewById(R.id.bt_cancel);
                //显示checkbox
                if (isShow) {
                    return false;
                } else {
                    isShow = true;
                    for (ItemBean bean : dataList) {
                        bean.setShow(true);
                    }
                    adapter.notifyDataSetChanged();
                }

                btn_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        linearLayout.setVisibility(View.GONE);
                        //取消时隐藏checkbox
                        cancle_Checkbox();
                    }
                });
                Button btn_delete=findViewById(R.id.bt_delete);
                //点击删除之后再次出现确认界面,防止误删
                btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
                        alertDialog.show();
                        alertDialog.setContentView(R.layout.long_del_pluse);
                        Window window = alertDialog.getWindow();
                        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
                        window.setGravity(Gravity.BOTTOM);
                        window.findViewById(R.id.text_cancle).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();
                                linearLayout.setVisibility(View.GONE);
                                cancle_Checkbox();
                            }
                        });
                        window.findViewById(R.id.text_delete).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Log.d("删除", "onActivityResult: ");
                                arrayList.remove(position);
                               adapter.notifyDataSetChanged();
                                //删除之后,对话框消失
                                alertDialog.dismiss();
                                linearLayout.setVisibility(View.GONE);
                            }
                        });
                    }
                });
                return true;
            }

        });

布局:

<?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:background="@drawable/back">

    <TextView
        android:id="@+id/text_1head"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="27dp"
        android:textSize="35dp"
        android:text="我的旅行日记"/>

    <FrameLayout
        android:id="@+id/frame_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_1head"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="35dp">

        <TextView
            android:id="@+id/text_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="日期"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/text_mood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="心情"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="天气"
            android:textSize="20dp" />
    </FrameLayout>

    <EditText
        android:id="@+id/edit_mystory"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/frame_1"
        android:layout_marginLeft="27dp"
        android:layout_marginRight="27dp"
        android:background="@drawable/round_edittext_journey"
        android:hint="写下我的故事........."/>
    <TextView
        android:id="@+id/text_past"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_mystory"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="27dp"
        android:layout_marginRight="27dp"
        android:textSize="25dp"
        android:text="过往"/>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="27dp"
        android:layout_marginRight="27dp"
        android:layout_below="@+id/text_past"/>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="#FAE7C8"
        android:visibility="gone" >

        <Button
            android:id="@+id/bt_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FAE7C8"
            android:layout_gravity="center"
            android:text="取消" />

        <TextView
            android:id="@+id/tv_sum"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="共计" />

        <Button
            android:id="@+id/bt_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FAE7C8"
            android:textColor="#F01F1F"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:text="删除" />
    </LinearLayout>
</RelativeLayout>


发布了46 篇原创文章 · 获赞 12 · 访问量 1591

猜你喜欢

转载自blog.csdn.net/weixin_43605701/article/details/103017191
今日推荐