Android总结之PopupWindow

PopupWindow 相信大家都不会陌生了。PopupWindows可以做出很多很好的效果。前几天做一个控件的时候正好用到了,而且也碰到了问题,今天正好就总结下,也算是一个总结。多总结才能更好的进步。

如何自定义PopupWindow的布局

这个问题相信大家都知道了,还是简单提一句。

  • 可以通过 setContentView() 方法来设置自定义布局
    /**
     * <p>Change the popup's content. The content is represented by an instance
     * of {@link android.view.View}.</p>
     *
     * <p>This method has no effect if called when the popup is showing.</p>
     *
     * @param contentView the new content for the popup
     *
     * @see #getContentView()
     * @see #isShowing()
     */
    public void setContentView(View contentView) {
        ···
    }
  • 还可以在 new 一个 PopupWindow 的时候将你需要的view作为一个参数传进去
    /**
     * <p>Create a new popup window which can display the <tt>contentView</tt>.
     * The dimension of the window must be passed to this constructor.</p>
     *
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     *
     * @param contentView the popup's content
     * @param width the popup's width
     * @param height the popup's height
     * @param focusable true if the popup can be focused, false otherwise
     */
    public PopupWindow(View contentView, int width, int height, boolean focusable) {
        ···
    }

通过查阅源码发现,构造函数里面还是通过调用 setContentView() 方法来设置布局的。

如何给PopupWindow设置弹出和消失的动画

这个其实蛮简单,通过 setAnimationStyle() 方法就好了,下面是源码

    /**
     * <p>Change the animation style resource for this popup.</p>
     *
     * <p>If the popup is showing, calling this method will take effect only
     * the next time the popup is shown or through a manual call to one of
     * the {@link #update()} methods.</p>
     *
     * @param animationStyle animation style to use when the popup appears
     *      and disappears.  Set to -1 for the default animation, 0 for no
     *      animation, or a resource identifier for an explicit animation.
     *      
     * @see #update()
     */
    public void setAnimationStyle(int animationStyle) {
        mAnimationStyle = animationStyle;
    }

细心查阅源码不难发现,最终这个 mAnimationStyle 通过 WindowManager.LayoutParams 设置给了PopupWindow 。

如何设置点击布局外部消失,控制 PopupWindow 消失的时机

其实这个并不是很复杂。

···
setOutsideTouchable(true);
···

这样点击 PopupWindow 外部就可以消失 PopupWindow 了, 但是有一个问题就是,消失是整个 PopupWindow 消失,当你需要 PopupWindow 里面的某个 item 先做完某个动画后再让 PopupWindow 消失,你需要添加一些代码了。

如何解决这个问题呢,其实有两个方法。
一个是

setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);

一定要记得 setBackgroundDrawable() 方法,一定要记得 setBackgroundDrawable() 方法,一定要记得 setBackgroundDrawable() 方法,重要的事情说3遍。为什么呢,我们看源码。

    /**
     * <p>Prepare the popup by embedding in into a new ViewGroup if the
     * background drawable is not null. If embedding is required, the layout
     * parameters' height is modified to take into account the background's
     * padding.</p>
     *
     * @param p the layout parameters of the popup's content view
     */
    private void preparePopup(WindowManager.LayoutParams p) {
        if (mContentView == null || mContext == null || mWindowManager == null) {
            throw new IllegalStateException("You must specify a valid content view by "
                    + "calling setContentView() before attempting to show the popup.");
        }

        if (mBackground != null) {
            final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            if (layoutParams != null &&
                    layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
                height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }

            // when a background is available, we embed the content view
            // within another view that owns the background drawable
            PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
            PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, height
            );
            popupViewContainer.setBackground(mBackground);
            popupViewContainer.addView(mContentView, listParams);

            mPopupView = popupViewContainer;
        } else {
            mPopupView = mContentView;
        }

        mPopupView.setElevation(mElevation);
        mPopupViewInitialLayoutDirectionInherited =
                (mPopupView.getRawLayoutDirection() == View.LAYOUT_DIRECTION_INHERIT);
        mPopupWidth = p.width;
        mPopupHeight = p.height;
    }

通过源码我们发现,设置了 mBackground 后,我们会新建一个 PopupViewContainer 出来,并将 PopupViewContainer 的背景设置为 mBackground 。最终显示的内容就是这个 PopupViewContainer ,那我们接下来看下 PopupViewContainer 时何方神圣。

    private class PopupViewContainer extends FrameLayout {
        private static final String TAG = "PopupWindow.PopupViewContainer";

        public PopupViewContainer(Context context) {
            super(context);
        }
        ×××
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if (getKeyDispatcherState() == null) {
                    return super.dispatchKeyEvent(event);
                }

                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getRepeatCount() == 0) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null) {
                        state.startTracking(event, this);
                    }
                    return true;
                } else if (event.getAction() == KeyEvent.ACTION_UP) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null && state.isTracking(event) && !event.isCanceled()) {
                        dismiss();
                        return true;
                    }
                }
                return super.dispatchKeyEvent(event);
            } else {
                return super.dispatchKeyEvent(event);
            }
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) {
                return true;
            }
            return super.dispatchTouchEvent(ev);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            if ((event.getAction() == MotionEvent.ACTION_DOWN)
                    && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
                dismiss();
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                dismiss();
                return true;
            } else {
                return super.onTouchEvent(event);
            }
        }
        ×××
    }

查看源码发现里面有一个 dispatchTouchEvent() 方法,这个方法用来拦截触摸事件的,仔细推敲我们就不难发现为什么要调用 setBackgroundDrawable() 方法了,不调用 setBackgroundDrawable() 方法,就没有 PopupViewContainer 对象,然后 dispatchTouchEvent() 当然就不会生效了。

还有一种方法就是写一个类继承 PopupWindow , 然后重写 dismiss() 方法。在 dismiss() 方法里面做完某个 item 的动画后,再做父类的 dismiss() 方法,让 PopupWindow 消失。
原因是点击外部的时候,PopupWindow的逻辑是走 dismiss() 方法的。在 dismiss() 里面 remove 掉 PopupWindow。我们可以利用这一点做文章。
大概的代码是

public class XPopupWindow extends PopupWindow {

    /**
    * 重写 dismiss() 方法,做完需要的动画后通过监听动画状态来让 PopupWindow 消失
    */
    @Override
    public void dismiss() {
        //tryDismissPopupWin ;
    }

    /**
    * 在动画结束调用父类的 dismiss() 方法来让 PopupWindow 消失
    */
    @Override
    public void onAnimationEnd(Animation animation) {
        super.dismiss();
    }
}

如何监听back按键,控制PopupWindow消失的时机

setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());

一定要记得 setBackgroundDrawable() 方法,一定要记得 setBackgroundDrawable() 方法,一定要记得 setBackgroundDrawable() 方法,重要的事情说3遍。相信这个是大家棘手的问题,我也是,我也碰到了。幸运的是我找到了解决方法。
在分析 “如何设置点击布局外部消失,控制PopupWindow消失的时机” 这里面做过详细的解释了。
在 PopupViewContainer 里面有个 dispatchKeyEvent() 方法,此方法会拦截back按键事件。

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if (getKeyDispatcherState() == null) {
                    return super.dispatchKeyEvent(event);
                }

                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getRepeatCount() == 0) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null) {
                        state.startTracking(event, this);
                    }
                    return true;
                } else if (event.getAction() == KeyEvent.ACTION_UP) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null && state.isTracking(event) && !event.isCanceled()) {
                        dismiss();
                        return true;
                    }
                }
                return super.dispatchKeyEvent(event);
            } else {
                return super.dispatchKeyEvent(event);
            }
        }

通过源码我们发现,最终走的还是 dismiss() 方法,所以我的解决方法同 “如何设置点击布局外部消失,控制 PopupWindow 消失的时机” 的讨论。采用继承的方式来实现。

先写到这里吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值