razerdp / BasePopup
- вторник, 25 декабря 2018 г. в 00:17:38
Java
亲,还在为PopupWindow烦恼吗?不如试试BasePopup,你会爱上他的~
1.x版本 | 2.x版本
致歉:非常抱歉~因为一时疏忽忘记合并一些东西,导致2.1.3版本在不拦截事件的情况下无anchorView弹窗会导致位置问题,在2.1.4重新合并了代码,对此造成的影响,深表歉意。在下表示以后一定会经过candy至少三次迭代后并检查完后发布release,避免发生类似错误。
WARN:
Android P已经适配,感谢@Guolei1130收集的方法。
文章地址:android_p_no_sdkapi_support
本库一开始采用360的方法,但不得不走Native,为了个Popup不得不引入so感觉很不值得,在看到这篇文章后,才想起UnSafe类,因此本库采用方法5。
如果以后UnSafe类移除掉的话,再考虑Native方法。
最后再一次感谢大牛提供的方法~
更多使用方法请查看Wiki#使用方法
Release | Candy |
---|---|
添加依赖到Gradle(请把{$latestVersion}替换成上面的Jcenter标签所示版本)
dependencies {
implementation 'com.github.razerdp:BasePopup:{$latestVersion}'
//candy版本
//implementation 'com.github.razerdp:BasePopup_Candy:{$latestVersion}'
}
从1.9.0-alpha开始支持背景模糊(只需要一个方法:setBlurBackgroundEnable(boolean)
)
RenderScript最低支持api 17(更低的情况将会使用fastblur),您需要在gradle配置一下代码
defaultConfig {
renderscriptTargetApi 25
renderscriptSupportModeEnabled true
}
像您平时定制View布局文件一样定制您的PopupWindow布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_dark"
android:orientation="vertical"
>
<TextView
android:id="@+id/tx_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="test1"
android:textColor="@color/color_black1"/>
</LinearLayout>
public class DemoPopup extends BasePopupWindow {
public DemoPopup(Context context) {
super(context);
}
@Override
public View onCreateContentView() {
return null;
}
}
强烈建议在onCreateContentView()
里使用createPopupById()
来进行inflate,这样本库才能正确的做出对应的解析和适配
public class DemoPopup extends BasePopupWindow {
public DemoPopup(Context context) {
super(context);
}
// 必须实现,这里返回您的contentView
// 为了让库更加准确的做出适配,强烈建议使用createPopupById()进行inflate
@Override
public View onCreateContentView() {
return createPopupById(R.layout.popup_normal);
}
// 以下为可选代码(非必须实现)
// 返回作用于PopupWindow的show和dismiss动画,本库提供了默认的几款动画,这里可以自由实现
@Override
protected Animation onCreateShowAnimation() {
return getDefaultScaleAnimation(true);
}
@Override
protected Animation onCreateDismissAnimation() {
return getDefaultScaleAnimation(false);
}
}
展示PopupWindow的方法有三种种,分别是showPopupWindow()
、showPopupWindow(View anchor)
和showPopupWindow(int x, int y)
:
new DemoPopup(getContext()).showPopupWindow();
//new DemoPopup(getContext()).showPopupWindow(v);
//new DemoPopup(getContext()).showPopupWindow(x,y);
这三个方法有不同的含义:
showPopupWindow()
:无参传入,此时PopupWindow参考对象为屏幕(或者说整个DecorView),Gravity的表现就像在FrameLayout里面的Gravity表现一样,表示其处于屏幕的哪个方位showPopupWindow(View anchor)
:传入AnchorView,此时PopupWindow参考对象为传入的anchorView,Gravity的表现则意味着这个PopupWindow应该处于目标AnchorView的哪个方位showPopupWindow(int x, int y)
:传入位置信息,此时PopupWindow将会在指定位置弹出。建议:如果PopupWindow需要重复展示或者保留状态,建议作为成员变量使用,而不要作为局部变量每次都创建
关于Gravity的更多api,请查看:Wiki-Api:Gravity
例子展示:
showPopupWindow()无参传入
gravity = CENTER 上述例子中xml写明了layout_gravity=center |
gravity = RIGHT | CENTER_VERTICAL |
---|---|
showPopupWindow(View v)传入anchorView
gravity = CENTER 上述例子中xml写明了layout_gravity=center |
gravity = RIGHT | CENTER_VERTICAL |
---|---|
showPopupWindow(int x, int y)传入位置x,y坐标
gravity = CENTER 上述例子中xml写明了layout_gravity=center |
---|
QuickPopupBuilder支持链式调用生成一个基于QuickPopup的PopupWindow,该Builder旨在快速建立一个简单的不包含复杂逻辑的PopupWindow,如上述案例,避免过于简单的PopupWindow也要继承BasePopupWindow,导致存在过多的类。
如果您并不需要很详细的定义一个PopupWindow,您也可以选择QuickPopupBuilder
采取链式写法快速编写出一个Popup以使用。
注意:默认QuickPopupBuilder.QuickPopupConfig配置中PopupWindow动画为淡入淡出
QuickPopupBuilder.with(getContext())
.contentView(R.layout.popup_normal)
.config(new QuickPopupConfig()
.gravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL)
.withClick(R.id.tx_1, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "clicked", Toast.LENGTH_LONG).show();
}
}))
.show();
//.show(anchorView);
show() | show(anchorView) |
---|---|
请看wiki(陆续完善中)
Link
【Candy】2.1.5-alpha2(2018/12/23)
delayInit()
方法来进行BasePopup的初始化【Candy】2.1.5-alpha(2018/12/23)
【Release】2.1.4(2018/12/21)
【Release】2.1.3(2018/12/21)
【Candy】2.1.3-alpha2(2018/12/20)
updatePopup()
方法即可~ClipToScreen()
修正不正确的问题【Candy】2.1.2-alpha2(2018/12/17)
ClipToScreen()
修正不正确的问题【Release】2.1.1(2018/12/13)
【Release】2.1.0(2018/12/12)
【Candy】2.1.0-betaX(2018/12)
GravityPopupFrag | LocatePopupFrag |
---|---|
![]() |
![]() |
AnyPosPopupFrag | UpdatePopupFrag |
![]() |
![]() |
BlurSlideFromBottomPopupFrag | CommentPopup |
![]() |
![]() |
SlideFromBottomPopup | InputPopup |
![]() |
![]() |
ListPopup | MenuPopup |
![]() |
![]() |
微信 | 支付宝 |
---|---|
![]() |
![]() |
因为目前还有朋友圈项目,建立了一个交流群,出于懒得管理那么多,所以如果有想法或者优化建议或者其他问题,欢迎加入“朋友圈交流群”
A:调用setBackgroundColor(Color.TRANSPARENT)或者setBackground(0)
A:调用dismiss(false)或者dismissWithOutAnimate()
A:设置setAllowDismissWhenTouchOutside(false)
A:PopupWindow需要windowToken,因此ApplicationContext或者Service里面是无法弹出的,建议通过发出事件通知栈顶Activity来弹出
A:设置setPopupWindowFullScreen(false)
A:设置setBackPressEnable(false)
A:在2.0.0到2.0.9之间,setAllowInterceptTouchEvent()不影响蒙层或背景,但从2.1.0开始,不再开启这个黑科技,而是选择跟官方保持同步,原因在于如果背景模糊或者有蒙层,那么该PopupWindow理应拦截事件,而不应该穿透事件,否则不应该拥有背景蒙层。
同时,因为系统PopupWindow默认是clipToScreen,也就是限制PopupWindow在屏幕内显示,当view边缘超过屏幕的时候,PopupWindow会反向移动以完整展示内容,因此如果您的PopupWindow需要突破屏幕显示(比如高度是全屏,但展示在某个view下面,此时bottom大于屏幕底部),请设置setClipToScreen(false)
。