PopWindowd窗

PopWindowd窗,第1张

实现效果:

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


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);//设置d窗位置

    }

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/872234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存