ListView点击item底部弹出popupWindow删除、修改、取消选择框

先看一下效果:
点击单个item弹出选择框,可以选择删除或者修改。删除单条item,或者跳转到修改页面。
popupWindow.gif
listview的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shipping_address_ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingLeft="@dimen/dp_15">

    <TextView
        android:id="@+id/shipping_address_name"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:text="姓名:张三"
        android:textSize="@dimen/sp_12" />

    <TextView
        android:id="@+id/shipping_address_phone"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:text="手机号:13800138000"
        android:textSize="@dimen/sp_12" />

    <TextView
        android:id="@+id/shipping_address_site"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:text="收货地址:北京市海淀区上地东二路上地佳园"
        android:textSize="@dimen/sp_12" />

</LinearLayout>

popupWindow的布局:
这里遇到个坑,如果不加上面透明的View的话,会出现,点击了一个item后,在不取消的情况下,再点击其它的item,会再弹出来一个popupWindow。造成重复叠加,再点删除的话无法判断是哪个item。设置无效。所有没办法只能加了个View,来把上面可聚焦的部分给遮盖掉。

<?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="match_parent"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/tv_delete"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text="删除"/>

        <Button
            android:id="@+id/tv_set"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text="修改"/>

        <Button
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text="取消"/>
    </LinearLayout>

popupWindow的Style:
这里引用了两个动画,一个是弹出的shipping_popup_show。和消失的shipping_popup_hide

    <!--popupwindow效果-->
    <style name="shipping_popup_style">
        <item name="android:windowFrame">@null</item>
        <item name="android:background">#00000000</item> <!-- 设置自定义布局的背景透明 -->
        <item name="android:windowEnterAnimation">@anim/shipping_popup_show</item>
        <item name="android:windowExitAnimation">@anim/shipping_popup_hide</item>
        <item name="android:windowBackground">@color/bgColor_overlay</item>  <!-- 设置window背景透明,也就是去边框 -->
        <item name="android:backgroundDimEnabled">false</item>

    </style>

在anim中创建两个文件
shipping_popup_show:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="120"
        android:toYDelta="0"
        android:duration="500"
        android:fillEnabled="true"
        android:fillAfter="true"/>
</set>

shipping_popup_hide:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="120"
        android:duration="500"
        android:fillEnabled="true"
        android:fillAfter="true"/>
</set>

Activity中listview的监听事件中:
这里在编辑事件跳转之前,一定要调用一下.dismiss。不然从详情页返回的时候popupWindow依然存在。

 @Override
    public void onItemClick(AdapterView<?> adapterView, final View view, final int i, long l) {
        View view1 = LayoutInflater.from(this).inflate(R.layout.popupwindow_layout,null);//PopupWindow对象
        popupWindow=new PopupWindow(this);
        //设置PopupWindow布局
        popupWindow.setContentView(view1);
        //设置动画
        popupWindow.setAnimationStyle(R.style.shipping_popup_style);
        //设置PopupWindow宽
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow高
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //在父布局的弹入/出位置
        rootView =LayoutInflater.from(this).inflate(R.layout.shipping_address, null);
        popupWindow.showAtLocation(rootView, Gravity.CENTER,0,0);
        //返回键点击,弹出
        popupWindow.setFocusable(false);
        //实例化控件
        bt_set= (Button) view1.findViewById(R.id.bt_set);
        bt_cancel= (Button) view1.findViewById(R.id.bt_cancel);
        bt_delete= (Button) view1.findViewById(R.id.bt_delete);

        bt_set.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        bt_delete.setOnClickListener(this);
        //删除
        bt_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                if (i<name.length) {
                    //删除该条item
                    list.remove(i);
                    //关闭PopupWindow
                    popupWindow.dismiss();
                }
                // 刷新布局
                adapter.notifyDataSetChanged();
            }
        });
        //编辑
        bt_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShippingAddressBean bean=list.get(i);
                Intent intent=new Intent(ShippingAddressActivity.this, ShippingAddressMinuteActivity.class);
                //封装数据包
                Bundle bundle = new Bundle();
                //传入对应的key,value
                bundle.putSerializable("date",bean);
                //发送到详情页
                intent.putExtras(bundle);
                popupWindow.dismiss();
                //执行 有返回结果的 跳转
                startActivityForResult(intent, 0);

            }
        });
        //取消
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });


    }

MinuteActivity中获取数据并展示
实体类继承Serializable方法。在这里可以用的到。下面是三个EditText编辑控件

      /**读取Activity传过来的数据 */
        Intent intent=getIntent();
        ShippingAddressBean notebean=(ShippingAddressBean) intent.getSerializableExtra("date");

        /**指定传过来的数据在当前页展示的位置*/
        shipping_minute_name.setText(notebean.getName());
        shipping_minute_phone.setText(notebean.getPhone());
        shipping_minute_site.setText(notebean.getAddress());

有不清楚的地方可以留言。

如有不妥,欢迎指正!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值