Android功能强大,UI 简洁,交互优雅的通用弹窗

new XPopup.Builder(getContext())

.asLoading(“正在加载中”)

.show();

// 这种弹窗从 1.0.0 版本开始实现了优雅的手势交互和智能嵌套滚动

new XPopup.Builder(getContext())

.asBottomList(“请选择一项”, new String[]{“条目 1”, “条目 2”, “条目 3”, “条目 4”, “条目 5”},

new OnSelectListener() {

@Override

public void onSelect(int position, String text) {

toast("click " + text);

}

})

.show();

  1. 显示依附于某个 View 或者某个点的弹窗

new XPopup.Builder(getContext())

.atView(v) // 依附于所点击的 View,内部会自动判断在上方或者下方显示

.asAttachList(new String[]{“分享”, “编辑”, “不带 icon”},

new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher},

new OnSelectListener() {

@Override

public void onSelect(int position, String text) {

toast("click " + text);

}

})

.show();

如果是想依附于某个 View 的触摸点,则需要先watch该 View,然后当单击或长按触发的时候去显示:

// 必须在事件发生前,调用这个方法来监视 View 的触摸

final XPopup.Builder builder = new XPopup.Builder(getContext())

.watchView(view.findViewById(R.id.btnShowAttachPoint));

view.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

builder.asAttachList(new String[]{“置顶”, “复制”, “删除”}, null,

new OnSelectListener() {

@Override

public void onSelect(int position, String text) {

toast("click " + text);

}

})

.show();

return false;

}

});

asAttachList方法内部是对 AttachPopupView 的封装,如果你的布局不是列表,可以继承 AttachPopupView 实现自己想要的布局。 AttachPopupView 会出现在目标的上方或者下方,如果你想要出现在目标的左边或者右边(像微信朋友圈那样点赞的弹窗),可以继承 HorizontalAttachPopupView,然后编写你的布局即可。

最简单示例如下:

public class CustomAttachPopup extends HorizontalAttachPopupView {

public CustomAttachPopup(@NonNull Context context) {

super(context);

}

@Override

protected int getImplLayoutId() {

return R.layout.custom_attach_popup;

}

@Override

protected void onCreate() {

super.onCreate();

findViewById(R.id.tv_zan).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

ToastUtils.showShort(“赞”);

}

});

findViewById(R.id.tv_comment).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

ToastUtils.showShort(“评论”);

}

});

}

}

  1. 关闭弹窗

dismiss();

  1. 自定义弹窗

当你自定义弹窗的时候,需要选择继承CenterPopupViewBottomPopupViewAttachPopupView/HorizontalAttachPopupViewDrawerPopupViewPartShadowPopupView其中之一。假设需要自定义 Center 类型的弹窗:

class CustomPopup extends CenterPopupView{

//自定义弹窗本质是一个自定义 View,但是只需重写这个构造,其他的不用重写

public CustomPopup(@NonNull Context context) {

super(context);

}

// 返回自定义弹窗的布局

@Override

protected int getImplLayoutId() {

return R.layout.custom_popup;

}

// 执行初始化操作,比如:findView,设置点击,或者任何你弹窗内的业务逻辑

@Override

protected void onCreate() {

super.onCreate();

findViewById(R.id.tv_close).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

dismiss(); // 关闭弹窗

}

});

}

// 设置最大宽度,看需要而定

@Override

protected int getMaxWidth() {

return super.getMaxWidth();

}

// 设置最大高度,看需要而定

@Override

protected int getMaxHeight() {

return super.getMaxHeight();

}

// 设置自定义动画器,看需要而定

@Override

protected PopupAnimator getPopupAnimator() {

return super.getPopupAnimator();

}

}

使用自定义弹窗:

new XPopup.Builder(getContext())

.asCustom(new CustomPopup(getContext()))

.show();

  1. 自定义动画

自定义动画已经被设计得非常简单,动画和弹窗是无关的;这意味着你可以将动画设置给内置弹窗或者自定义弹窗。继承PopupAnimator,实现 3 个方法:

  • 如何初始化动画

  • 动画如何开始

  • 动画如何结束

比如:自定义一个旋转的动画:

class RotateAnimator extends PopupAnimator{

@Override

public void initAnimator() {

targetView.setScaleX(0);

targetView.setScaleY(0);

targetView.setAlpha(0);

targetView.setRotation(360);

}

@Override

public void animateShow() {

targetView.animate().rotation(0).scaleX(1).scaleY(1).alpha(1).setInterpolator(new FastOutSlowInInterpolator()).setDuration(animateDuration).start();

}

@Override

public void animateDismiss() {

targetView.animate().rotation(360).scaleX(0).scaleY(0).alpha(0).setInterpolator(new FastOutSlowInInterpolator()).setDuration(animateDuration).start();

}

}

使用自定义动画:

new XPopup.Builder(getContext())

.customAnimator(new RotateAnimator())

.asConfirm(“演示自定义动画”, “当前的动画是一个自定义的旋转动画,无论是自定义弹窗还是自定义动画,已经被设计得非常简单;这个动画代码只有 6 行即可完成!”, null)

.show();

  1. 显示 DrawerLayout 类型弹窗

对于 DrawerLayout 类型的弹窗,我只能帮你做好弹窗效果和手势交互。里面的 UI 和逻辑是无法帮你完成的,所以需要自定义一个弹窗,继承DrawerPopupView。代码非常简单,如下:

public class CustomDrawerPopupView extends DrawerPopupView {

public CustomDrawerPopupView(@NonNull Context context) {

super(context);

}

@Override

protected int getImplLayoutId() {

return R.layout.custom_drawer_popup;

}

@Override

protected void onCreate() {

super.onCreate();

findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getContext(), “nothing!!!”, Toast.LENGTH_SHORT).show();

}

});

}

}

使用自定义的 DrawerLayout 弹窗:

new XPopup.Builder(getContext())

.popupPosition(PopupPosition.Right)//右边

.hasStatusBarShadow(true) //启用状态栏阴影

.asCustom(new CustomDrawerPopupView(getContext()))

.show();

  1. 自定义局部阴影弹窗

这种效果从分类上看仍然是 Attach 类型,因为要依附于某个 View,在其上方或者下方显示。常见于列表条件筛选弹窗,比如京东或者淘宝的商品列表筛选。同样我只能帮你把复杂的交互效果做了,弹窗里面的 UI 和逻辑需要你自己继承PartShadowPopupView来做,这当然非常简单。 最简单的示例如下:

public class CustomPartShadowPopupView extends PartShadowPopupView {

public CustomPartShadowPopupView(@NonNull Context context) {

super(context);

}

@Override

protected int getImplLayoutId() {

return R.layout.custom_part_shadow_popup; // 编写你自己的布局

}

@Override

protected void onCreate() {

super.onCreate();

// 实现一些 UI 的初始和逻辑处理

}

}

显示的时候仍然需要指定 atView 显示,内部会智能判断应该如何展示以及使用最佳的动画器:

new XPopup.Builder(getContext())

.atView(ll_container)

.asCustom(new CustomPartShadowPopupView(getContext()))

.show();

  1. 自定义 Bottom 类型的弹窗

自定义 Bottom 类型的弹窗会比较常见,默认 Bottom 弹窗带有手势交互和嵌套滚动;如果您不想要手势交互可以调用enableDrag(false)方法关闭。

如果弹窗内有输入框,在弹出输入法的情况下,弹窗默认会贴附在输入法之上,并且保证不会盖住输入框;目前 Center 和 Bottom 类型弹窗有此效果。

请注意:弹窗的宽高是自适应的,大部分情况下都应该将弹窗布局的高设置为wrap_content;除非你希望得到一个高度撑满的弹窗。

Demo 中有一个模仿知乎评论的实现,代码在这里:

public class ZhihuCommentPopup extends BottomPopupView {

VerticalRecyclerView recyclerView;

public ZhihuCommentPopup(@NonNull Context context) {

super(context);

}

@Override

protected int getImplLayoutId() {

return R.layout.custom_bottom_popup;

}

@Override

protected void onCreate() {

super.onCreate();

recyclerView = findViewById(R.id.recyclerView);

ArrayList strings = new ArrayList<>();

for (int i = 0; i < 30; i++) {

strings.add(“”);

}

CommonAdapter commonAdapter = new CommonAdapter(R.layout.adapter_zhihu_comment, strings) {

@Override

protected void bind(@NonNull ViewHolder holder, @NonNull String s, int position) {}

};

commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener(){

@Override

public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

dismiss();

}

});

recyclerView.setAdapter(commonAdapter);

}

// 最大高度为 Window 的 0.85

@Override

protected int getMaxHeight() {

return (int) (XPopupUtils.getWindowHeight(getContext())*.85f);

}

}

  1. 大图浏览弹窗

这种弹窗多用于 App 内列表中图片进行详细展示的场景,用法如下:

// 多图片场景

new XPopup.Builder(getContext()).asImageViewer(imageView, position, list, new OnSrcViewUpdateListener() {

@Override

public void onSrcViewUpdate(ImageViewerPopupView popupView, int position) {

// 作用是当 Pager 切换了图片,需要更新源 View

popupView.updateSrcView((ImageView) recyclerView.getChildAt(position));

}

}, new ImageLoader())

.show();

// 单张图片场景

new XPopup.Builder(getContext())

.asImageViewer(imageView, url, new ImageLoader())

.show();

// 图片加载器,我不负责加载图片,需要你实现一个图片加载器传给我,这里以 Glide 为例。

class ImageLoader implements XPopupImageLoader {

@Override

public void loadImage(int position, @NonNull String url, @NonNull ImageView imageView) {

Glide.with(imageView).load(url).into(imageView);

}

//必须实现这个方法,返回 uri 对应的缓存文件,可参照下面的实现,内部保存图片会用到。

@Override

public File getImageFile(@NonNull Context context, @NonNull Object uri) {

try {

return Glide.with(context).downloadOnly().load(uri).submit().get();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

注意事项:假设你使用 Glide 加载图片,如果你的 ImageView 是 CenterCrop 的,那么加载的时候一定要指定大小为Target.SIZE_ORIGINAL; 这样会禁止 Glide 裁剪图片,保证可以拿到原始图片,让图片过渡动画变的天衣无缝。例如:

Glide.with(imageView).load(s).apply(new RequestOptions().override(Target.SIZE_ORIGINAL))

.into(imageView);

如果你使用其他类库加载图片,请保证加载的图片没有被裁剪过。

  1. 其他

  2. 设置主色调

默认情况下,XPopup 的主色为灰色,主色作用于 Button 文字,EditText 边框和光标,Check 文字的颜色上。主色调只需要设置一次即可,可以放在 Application 中设置。

XPopup.setPrimaryColor(getResources().getColor(R.color.colorPrimary));

  1. 设置全局的动画时长
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值