XPopup 从接入到落地:新手如何 30 分钟搞定 Android 弹窗
2026/9/21 16:07:25 网站建设 项目流程

XPopup 从接入到落地:新手如何 30 分钟搞定 Android 弹窗

【免费下载链接】XPopup🔥XPopup2.0版本重磅来袭,2倍以上性能提升,带来可观的动画性能优化和交互细节的提升!!!功能强大,交互优雅,动画丝滑的通用弹窗!可以替代Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner等组件,自带十几种效果良好的动画, 支持完全的UI和动画自定义!(Powerful and Beautiful Popup for Android,can absolutely replace Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner. With built-in animators , very easy to custom popup view.)项目地址: https://gitcode.com/GitHub_Trending/xpo/XPopup

本指南面向准备把弹窗接入实际业务的 Android 开发者:XPopup 是一个 Android 弹窗组件库,目标是替代 Dialog、PopupWindow、PopupMenu、BottomSheet、DrawerLayout 和 Spinner。全文只给最小可用代码,读完你可以完成接入、选型和自定义弹窗开发。

XPopup 把弹窗分成 Center、Bottom、Attach、Drawer、FullScreen、Position、ImageViewer 等几类,每类都有内置实现和可继承的基础类,动画与手势、软键盘适配、生命周期管理都封装好了。先看几组真实效果(Demo 工程源码在 app/src/main/java/com/lxj/xpopupdemo/ 下可对照阅读):

三步接入 XPopup:最快配置方法

读完这节,你可以在自己的工程里弹出第一个弹窗。

第 1 步:配置仓库。XPopup 发布在 jitpack 上,工程根目录的build.gradle需要加仓库:

allprojects { repositories { maven { url 'https://jitpack.io' } } }

第 2 步:加依赖。模块build.gradle中添加 XPopup 及三个必需库(appcompat、material、recyclerview):

dependencies { implementation 'com.github.li-xiaojun:XPopup:2.10.15' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.recyclerview:recyclerview:1.2.1' }

注意compileSdkVersion必须 ≥ 29,否则编译不过。

第 3 步:弹出第一个弹窗。一个确认框,五行代码:

new XPopup.Builder(this).asConfirm("提示", "确定要删除这一项吗?", new OnConfirmListener() { @Override public void onConfirm() { // 这里执行你的删除逻辑 } }).show();

弹窗到底选哪个?按业务场景查表

读完这节,你拿到任何需求都能直接映射到对应方法。先记住这张选型表:

业务场景内置方法继承基础类
确认 / 二次确认asConfirmCenterPopupView
加载提示asLoadingLoadingPopupView
列表选择(底部)asBottomListBottomPopupView
列表选择(居中)asCenterListCenterListPopupView
锚点菜单 / 气泡asAttachListAttachPopupView
侧边抽屉asDrawerLeft/asDrawerRightDrawerPopupView
输入框确认asInputConfirmInputConfirmPopupView

内置弹窗还支持bindLayoutId参数,直接复用你项目里已有的布局,动画和交互照旧生效。

确认与加载提示:Center 系列

适用:删除确认、退出确认、等待网络返回的 Loading。

// 确认弹窗 new XPopup.Builder(this) .asConfirm("提示", "确定要退出吗?", null) // 第二个参数传 null 隐藏取消按钮 .show(); // 加载弹窗 new XPopup.Builder(this).asLoading("加载中...").show();

💡 加载弹窗要配合autoDismiss(false)手动dismiss(),否则确认后会自动消失。

列表选择:Bottom 系列(底部弹窗示例)

适用:分享面板、城市/筛选选择。手势拖拽关闭、嵌套滚动库内部已处理,你只关心回调。

new XPopup.Builder(this) .asBottomList("请选择城市", new String[]{"北京", "上海", "广州"}, new OnSelectListener() { @Override public void onSelect(int position, String text) { // text 是选中项,position 是下标 } }) .show();

需要单选勾选效果时,用带checkedPosition的重载;列表数据来自接口时,继承BottomPopupView自己填充即可(见下一节)。

锚点菜单:Attach 系列

适用:点某个图标后弹出的操作菜单、输入联想(类似系统 PopupMenu,但可任意自定义 UI)。核心是告诉 Builder 锚点是谁:

new XPopup.Builder(this) .atView(anchorView) // 弹窗依附于这个 View .asAttachList(new String[]{"分享", "收藏", "举报"}, null, new OnSelectListener() { @Override public void onSelect(int position, String text) { // 处理选中项 } }) .show();

智能定位已经内置:屏幕边缘会自动翻边,不会把菜单顶出屏幕。

侧边抽屉:Drawer 系列

适用:个人中心侧滑栏、筛选面板。与页面布局完全解耦,任何界面都能加。

new XPopup.Builder(this) .asDrawerLeft(new CustomDrawerPopupView(this)) // 右侧用 asDrawerRight .show();

抽屉内容就是一个普通布局(继承DrawerPopupView),支持手势拖拽收起、状态栏阴影等细节。

自定义弹窗开发:两步改造路径

读完这节,你能把任意布局变成受 XPopup 管理的弹窗。路径只有两步:建布局 → 继承基础类

第 1 步,写布局文件(比如my_login_popup.xml),想放什么放什么。

第 2 步,继承与场景匹配的基础类,只覆写两个点:

public class LoginPopup extends CenterPopupView { public LoginPopup(@NonNull Context context) { super(context); } @Override protected int getImplLayoutId() { return R.layout.my_login_popup; // 你的布局 } @Override protected void onCreate() { super.onCreate(); // 像写 Activity 一样:找控件、绑事件 findViewById(R.id.btn_confirm).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dismiss(); // 关闭弹窗 } }); } }

然后像内置弹窗一样使用:

new XPopup.Builder(this) .asCustom(new LoginPopup(this)) .isDestroyOnDismiss(true) // 只弹一次的弹窗建议加这句,用完即回收 .show();

onShow()/onDismiss()两个回调对应弹窗的显示与消失时机。基础类都在 library/src/main/java/com/lxj/xpopup/core/,完整写法可对照 Demo 里的 CustomDrawerPopupView.java。

动画与交互:4 个默认行为 + 1 个开关

读完这节,你无需额外代码就能获得手势、键盘、生命周期管理,还能一键换动画。

内置动画。Builder 一行切换,可用值见 PopupAnimation.java:

.popupAnimation(PopupAnimation.ScaleAlphaFromCenter) // 中心缩放+渐变 .popupAnimation(PopupAnimation.NoAnimation) // 一键关闭动画

自定义动画。继承PopupAnimator实现三个方法即可,一个旋转动画示例(来自 CustomAnimatorDemo.java):

class RotateAnimator extends PopupAnimator { @Override public void initAnimator() { targetView.setScaleX(0); targetView.setScaleY(0); targetView.setAlpha(0); } @Override public void animateShow() { targetView.animate().scaleX(1).scaleY(1).alpha(1).setDuration(300).start(); } @Override public void animateDismiss() { targetView.animate().scaleX(0).scaleY(0).alpha(0).setDuration(300).start(); } }

挂上用customAnimator(new RotateAnimator())。现成动画器都在 library/src/main/java/com/lxj/xpopup/animator/。

以下三件事是默认行为,不用你写代码:

  1. 手势关闭:底部弹窗上滑关闭、居中弹窗下滑关闭、抽屉拖拽收起,均开箱即用;不想拦截触摸时用dismissOnTouchOutside(false)调整。
  2. 软键盘适配:弹窗内含EditText时,XPopup 自动把弹窗顶在键盘上方,不会被遮挡。
  3. 生命周期管理:Builder 默认监听宿主生命周期,Activity/Fragment 销毁时自动释放;XPopup 本身实现了 LifecycleOwner,可以直接被 LiveData 观察,弹窗不可见时不更新数据。在 Fragment 中弹窗时把 Fragment 传给 Builder 即可:new XPopup.Builder(fragment)

XPopup 避坑清单:现象 → 原因 → 解决

发布前逐项核对,以下问题都是接入期高频踩坑:

现象原因解决
弹窗不显示非主线程调用show(),或宿主已销毁主线程调用;确认 Activity/Fragment 处于活跃状态
依赖解析失败未配 jitpack 仓库build.gradlemaven { url 'https://jitpack.io' }
升级后编译报错compileSdkVersion低于 29提升到 29 及以上
与既有库版本冲突XPopup 传递依赖了 subsampling-scale-image-view依赖声明里exclude group: "com.davemorrissey.labs", module: "subsampling-scale-image-view-androidx"
混淆后线上崩溃widget包被移除按 README.md 混淆章节添加 keep 规则(-keep class com.lxj.xpopup.widget.**{*;}
底部弹窗里列表滚动异常用了普通 RecyclerView,嵌套滚动未适配优先使用库内VerticalRecyclerView或内置列表实现
反复弹同一弹窗后内存不降每次 new 新实例且未回收单次使用的弹窗设置isDestroyOnDismiss(true)
弹窗内容过长、层级过深布局嵌套太深,动画期间执行耗时逻辑精简布局层级;动画期间不做 IO / 网络

⚠️ 混淆规则、exclude 写法等细节以仓库 README.md 的"混淆"章节为准,发版前务必回归一次弹窗全流程。

落地建议

XPopup 的价值在于:弹窗选型、动画、手势、键盘和生命周期这五件事你一次性都不用再操心,剩下的只是写布局。

下一步:打开仓库的 Demo 模块 app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java,按你业务里真实存在的一个弹窗(建议从确认框或底部列表开始)照抄一遍,跑通后再替换成自己的布局和数据。

【免费下载链接】XPopup🔥XPopup2.0版本重磅来袭,2倍以上性能提升,带来可观的动画性能优化和交互细节的提升!!!功能强大,交互优雅,动画丝滑的通用弹窗!可以替代Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner等组件,自带十几种效果良好的动画, 支持完全的UI和动画自定义!(Powerful and Beautiful Popup for Android,can absolutely replace Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner. With built-in animators , very easy to custom popup view.)项目地址: https://gitcode.com/GitHub_Trending/xpo/XPopup

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询