PopWindow弹窗

实现效果:

单独封装PopWindow类,点击类目内容回填,弹窗渐变,弹窗后主界面失去焦点并变暗


PopWindow类抽取

新建一个ShowPopWindow.java
popwindow样式

<?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="wrap_content"
    android:background="@drawable/shape_listview"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lv_ck"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:cacheColorHint="#00000000"
        android:overScrollMode="never" />


    <TextView
        android:id="@+id/cancel_pop"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/shape_corner"--这是圆角矩形样式
        android:gravity="center"
        android:text="取消"
        android:textColor="@color/black" />

</LinearLayout>

shape_listview 渐变样式

	<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@color/white" //白灰渐变
        android:endColor="@color/slateGray"
        android:angle="270"
        />

<!--    <solid android:color="@color/white" />-->
    <corners
        android:bottomRightRadius="4dip"
        android:bottomLeftRadius="4dip"
        android:topLeftRadius="4dip"
        android:topRightRadius="4dip"
        />
    <corners android:radius="20dp"/>
    <stroke android:width="1dp" android:color="@color/black" />
</shape>
public class ShowPopWindow {
    private final Context mContext;
    private final Window mWindow;
    private final ArrayAdapter mAdapter;
    private final int res;
    private PopupWindow popupWindow;
    private View popView;
    private int clickPos = -1;


    public ShowPopWindow(Context context, int convertViewResource, Window window, ArrayAdapter adapter) {
        this.mContext = context;
        this.mWindow = window;
        this.res = convertViewResource;
        this.mAdapter = adapter;
    }

    public void initPopWindow() {
        popView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow, null);
        View rootView = LayoutInflater.from(mContext).inflate(res, null);

        TextView cancle = popView.findViewById(R.id.cancel_pop);
        ListView lv_ck = popView.findViewById(R.id.lv_ck);
        lv_ck.setAdapter(mAdapter);

        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(null);
        popupWindow.setAnimationStyle(com.sab.libui.baseui.R.style.PopAnimation);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);

        //设置背景变暗
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.alpha = 0.7f;
        mWindow.setAttributes(lp);

		//在主activity写使其回写点击内容
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = mWindow.getAttributes();
//                lp.alpha = 1f;
//                mWindow.setAttributes(lp);
//            }
//        });

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickPos = -1;
                lp.alpha = 1f;
                popupWindow.dismiss();
            }
        });

        lv_ck.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickPos = position;
                lp.alpha = 1f;
                popupWindow.dismiss();
            }
        });

        popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, -50);//设置弹窗位置

    }

    public PopupWindow getPopWin() {
        return popupWindow;//返回弹窗对面用于调用界面监听弹窗关闭事件,回填内容
    }

    public View getPopView() {
        return popView;
    }

    public int getClickPos() {
        return clickPos;//返回点击的position
    }
}

调用方法及点击回填事件

//创建对象
Window mWindow=getWindow();
showPopWindow = new ShowPopWindow(GoodsMoveActivity.this, 使用该组件的界面, mWindow, adapter);

showPopWindow.initPopWindow();
PopupWindow popWin = showPopWindow.getPopWin();
View popView = showPopWindow.getPopView();

popWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
 @Override
 public void onDismiss() {
     WindowManager.LayoutParams lp = mWindow.getAttributes();
     lp.alpha = 1f;
     mWindow.setAttributes(lp);
     int clickPos = showPopWindow.getClickPos();
     if (clickPos != -1) {
         String item = adapter.getItem(clickPos);
         vb.etCk.setText(item);
     }
 }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值