PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用

PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用###


GitHub 地址:PopupWindowFragment

Bug反馈地址: [email protected]
简介:

  • PopupWindowFragment是为了在Fragment模型中更好,更方便的使用PopupWindow而存在的通用组件。跟DialogFragment类似,也是将一个新的Window对象纳入Fragment的状态管理。但是由于DialogFragment的功能及接口的局限性,已经很难满足更加复杂的界面要求。因此,使用接口更加丰富的PopupWindow代替Dialog的方式应运而生。

  • 在PopupWindowFragment中,你除了可以轻松的指定Window切换时候的过场动画,还可以动态指定背景及布局外,在屏幕翻转的时候,PopupWindowFragment一样会记录翻转前的状态。

1.使用方法:

/**生成一个PopupWindowFragment
* @code david.demos.popupwindow.DemoPopupWindowFragment
*/
public class DemoPopupWindowFragment extends PopupWindowFragment {    
    @Override    
    protected View onCreateView(LayoutInflater inflater, Bundle savedInstanceState) {       
     return inflater.inflate(R.layout.demo_pop_window,null);   
    }
}

/**Activity中调用
* @code david.demos.popupwindow.Demos_PopupWindowFragment
*/
private PopupWindowFragment mPopupWindowFragment = new DemoPopupWindowFragment();
...
mPopupWindowFragment.show(getFragmentManager(),mPopTag);

调用PopupWindowFragment的方法就是如此简单。当然,这部分剪口很大一部分参考了DialogFragment的接口设计。调用上述方法后可以实现效果:

PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用_第1张图片
弹出一个PopupWindowFragment

2.过场动画:

当然PopupWindowFragment可以做的事情远不止此,PopupWindowFragment可以通过简单的request方法来定制你的过场动画:

/**Activity中调用
* @code david.demos.popupwindow.Demos_PopupWindowFragment
*/
private int[] mWindowAnims =  { 
       PopupWindowFragment.ANIM_ACTIVITY,        
       PopupWindowFragment.ANIM_TOAST,        
       PopupWindowFragment.ANIM_DIALOG,        
       PopupWindowFragment.ANIM_INPUTMETHOD
};
...
mPopupWindowFragment.requestPopupAnimationStyle(mWindowAnims[index]);
...

PopupWindowFragment将默认提供4个过场动画,分别是Activity动画,Toast动画,Dialog弹出动画,还有输入法动画,效果如下:

PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用_第2张图片
指定不同的过场动画


3.状态保存:

PopupWindowFragment拥有Fragment的保存特质,可以在Activity切换的时候依旧保持自己的状态:

PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用_第3张图片
Activity重新生成后,PopupWindowFragment依旧能保持之前的状态和属性

4.定制Gravity:

//demo/Demos_PopupWindowFragment.java

private int[] mGravitys =  {Gravity.BOTTOM,Gravity.CENTER,Gravity.TOP};
...
mPopupWindowFragment.requestGravity(mGravitys[i]);
....

效果:

PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用_第4张图片
通过Gravity指定位置

PopupWindowFragment提供了丰富的API和接口供各位开发者使用,如果疑问和Bug,请发送到作者邮箱。thx

你可能感兴趣的:(PopupWindowFragment :使用Fragment来管理和生成PopupWindow,并且提供更好的定制化接口,比DialogFragment更加强大实用)