该类在3.0版本后删除,3.0之后的版本请直接依赖BasePopupWindow,使用上跟本文档一致。
具体文档请查阅:关于BasePopup 3.0的破坏性更新说明


原文档(3.0之前的版本使用)

有的时候我们需要在PopupWindow初始化之前执行某些操作,比如根据某个值来决定返回不同的ContentView。
由于BasePopupWindow的初始化是在构造器进行InflateView的,因此我们无法提前干预,如下述例子:

  1. public class TestPopup extends BasePopupWindow {
  2. int flag;
  3. public TestPopup(Context context, int flag) {
  4. //在super(context)中就已经调用了#onCreateContentView
  5. //我们无法在#onCreateContentView之前完成flag的赋值
  6. super(context);
  7. this.flag = flag;
  8. }
  9. @Override
  10. public View onCreateContentView() {
  11. return flag == 0 ? createPopupById(R.layout.inflate_a) : createPopupById(R.layout.inflate_b);
  12. }
  13. }

为了解决上述问题,在2.2.2版本开始,我们提供了BaseLazyPopupWindow,使用非常简单,修改上述继承的基类为BaseLazyPopupWindow即可。

提示
BaseLazyPopupWindow将会延迟到您调用showPopupWindow()之后才进行初始化,因此,您的View操作(如FindView、setxxx等等)请务必放到onViewCreated中。
延迟到showPopupWindow()后初始化则意味着当您调用了弹窗方法后才开始初始化(包括popupwindow的创建,view.inflate等),所以在很复杂的弹窗下可能会出现卡顿。