Android自定义控件AndroidAndroid

Android PopupWindow使用

2020-08-18  本文已影响0人  可乐_JS
效果图
package ***;

import android.view.View;
import android.view.ViewGroup;

/**
 * Des: PopupWindow管理类
 * Created by kele on 2020/7/21.
 * E-mail:984127585@qq.com
 */
public class PopupWindowManager {

    private MyPopupWindow pw;

    /**
     * 构造私有
     */
    private PopupWindowManager() {
    }

    /**
     * 匿名内部内实现单例
     */
    private static class PopupWindowManagerHolder {
        private static final PopupWindowManager INSTANCE = new PopupWindowManager();
    }

    public static PopupWindowManager getInstance() {
        return PopupWindowManagerHolder.INSTANCE;
    }

    /**
     * 初始宽度
     */
    private static final int POP_WIDTH = ViewGroup.LayoutParams.MATCH_PARENT;
    /**
     * 初始高度
     */
    private static final int POP_HEIGHT = ViewGroup.LayoutParams.WRAP_CONTENT;

    /**
     * 初始化 使用默认宽高
     *
     * @param contentView PopupWindow需要展示的view
     * @return
     */
    public PopupWindowManager init(View contentView) {
        return init(contentView, POP_WIDTH, POP_HEIGHT);
    }

    /**
     * 初始化 使用自定义宽高
     *
     * @param contentView PopupWindow需要展示的view
     * @param width       宽
     * @param height      高
     * @return
     */
    public PopupWindowManager init(View contentView, int width, int height) {
        pw = new MyPopupWindow(contentView, width, height, true);
        pw.setOutsideTouchable(true);
        pw.setFocusable(true);
        pw.setElevation(0);
        pw.setTouchable(true);
        return this;
    }

    /**
     * 设置关闭监听
     *
     * @param listener
     */
    public void setPopDismissListener(MyPopupWindow.OnDismissListener listener) {
        if (null == pw) {
            return;
        }
        pw.setOnDismissListener(listener);
    }

    /**
     * 显示在指定的targetView的指定位置 无偏移量
     *
     * @param targetView 目标view
     * @param gravity    位置
     */
    public void showAtLocation(View targetView, int gravity) {
        showAtLocation(targetView, gravity, 0, 0);
    }

    /**
     * 显示在指定的targetView的指定位置 带偏移量
     *
     * @param targetView 目标view
     * @param gravity    位置
     * @param x          x轴偏移量
     * @param y          y轴偏移量
     */
    public void showAtLocation(View targetView, int gravity, int x, int y) {
        if (null == pw) {
            return;
        }
        pw.showAtLocation(targetView, gravity, x, y);
    }

    /**
     * 显示在指定的targetView底部 无偏移量
     *
     * @param targetView 目标view
     */
    public void showAsDropDown(View targetView) {
        if (null == pw) {
            return;
        }
        pw.showAsDropDown(targetView);
    }

    /**
     * 显示在指定的targetView底部 有偏移量
     *
     * @param targetView 目标view
     * @param offX       x轴偏移量
     * @param offY       y轴偏移量
     */
    public void showAsDropDown(View targetView, int offX, int offY) {
        if (null == pw) {
            return;
        }
        pw.showAsDropDown(targetView, offX, offY);
    }

    /**
     * 显示在指定的targetView的指定位置
     *
     * @param targetView 目标view
     * @param offX       x轴偏移量
     * @param offY       y轴偏移量
     * @param gravity    位置
     */
    public void showAsDropDown(View targetView, int offX, int offY, int gravity) {
        if (null == pw) {
            return;
        }
        pw.showAsDropDown(targetView, offX, offY, gravity);
    }

    /**
     * 关闭
     */
    public void dismiss() {
        if (null == pw) {
            return;
        }
        pw.dismiss();
    }
}
package ***;

import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;

/**
 * Des: 自定义PopupWindow
 * Created by kele on 2020/7/21.
 * E-mail:984127585@qq.com
 */
public class MyPopupWindow extends PopupWindow {

    public MyPopupWindow(View contentView, int width, int height, boolean b) {
        super(contentView, width, height, b);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int heightPixels = anchor.getResources().getDisplayMetrics().heightPixels;
            int h = heightPixels - rect.bottom;
            //设置负值能达到自适应的效果
            setHeight(-20);
        }
        super.showAsDropDown(anchor);
    }
}

注意:MyPopupWindow中的整体根布局的高度(如上图中上部分+带半透明黑色背景部分)需要在显示之前给设置一个小于-2的值才能达到宽度真正自适应的效果。这是经过多次的尝试后得到的结果,具体原因本人还未知,知道的可以留言告知下

    /**
     * 显示
     *
     * @param targetView 需要显示的内容view
     * @return
     */
    public TimeSelectPopHolder show(View targetView) {
        //显示pop
        PopupWindowManager.getInstance().dismiss();
        PopupWindowManager.getInstance().init(selectTime).showAsDropDown(targetView);
        PopupWindowManager.getInstance().setPopDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (null == mCallback) {
                    return;
                }
                mCallback.onDismiss();
            }
        });
        return this;
    }
更新-2020.09.14

更改下之前对PopupWindow高度问题的理解
1、本文上面说的关于高度自适应的问题,在我今天的撸码中出现了问题,和之前的理解有点不一样。今天的总结是:当你给pop设置了高度matchparent后,它不会出现在你要显示的view的下面,而是充满全屏,这个问题通过上面说的自定义重写pop的showAsDropDown方法是可以解决的,代码如下:

    @Override
    public void showAsDropDown(View anchor) {
            if (Build.VERSION.SDK_INT >= 24) {
                Rect rect = new Rect();
                anchor.getGlobalVisibleRect(rect);
                int heightPixels = anchor.getResources().getDisplayMetrics().heightPixels;
                int h = heightPixels - rect.bottom;
                setHeight(h);
            }
        super.showAsDropDown(anchor);
    }

这样pop就可以在你需要显示的view下方以占据下半部分全部屏幕的方式来展现了!

上一篇 下一篇

猜你喜欢

热点阅读