public void onConfirm() {
toast(“click confirm”);
}
})
.show();
new XPopup.Builder(getContext()).asInputConfirm(“我是标题”, “请输入内容。”,
new OnInputConfirmListener() {
@Override
public void onConfirm(String text) {
toast("input text: " + text);
}
})
.show();
new XPopup.Builder(getContext())
//.maxWidth(600)
.asCenterList(“请选择一项”, new String[]{“条目 1”, “条目 2”, “条目 3”, “条目 4”},
new OnSelectListener() {
@Override
public void onSelect(int position, String text) {
toast("click " + text);
}
})
.show();
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();
- 显示依附于某个 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 Cont