09 监听及回调
## 翻页回调
页码发生变更时,监听返回此时页码数据
**代码示例:**
```
mPdfView.setPDFEventCallback(callback);
PDFViewEventCallback callback = new PDFViewEventCallback() {
@Override
public void onPageIndexChanged(final int page,final int totalPage) {
VerticalPdfViewActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (tvPageNumber != null){
// 设置页面显示页码
String indexShow = " " + (page + 1) + "/" + mPdfView.getCurrentFilePageCount() + " ";
int indexof = indexShow.indexOf("/");
SpannableStringBuilder builder = new SpannableStringBuilder(indexShow);
builder.setSpan(redSpan, 0, indexof, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvPageNumber.setText(builder);
}
}
});
}
};
```
**接口参数说明:**
- callback
类型:PDFViewEventCallback
说明:页面变更回调函数
page:当前页码
totalPage:总页码
## 长按监听
在阅读模式,长按屏幕时,返回此时屏幕坐标,用户添加签名数据,则会放置在长按位置处。
**代码示例:**
```
//阅读模式时 长按监听
long pressTime=1500; //长按效果触发时间 单位ms
mPdfView.setLongPressListener(pressTime,longPressListener);
//长按监听
LongPressListener longPressListener=new LongPressListener() {
@Override
public void onLongPressListener(float screenX, float screenY) {
//短按隐藏
if(screenX<0&&screenY<0){
if (signManagerTipBar != null && signManagerTipBar.isShowing()){
signManagerTipBar.dismiss();
}
return;
}
if (signManagerTipBar != null && signManagerTipBar.isShowing()){
signManagerTipBar.dismiss();
}
signManagerTipBar = new Dialog(mContext, R.style.navdialog);
Window dialogWindow = signManagerTipBar.getWindow();
WindowManager.LayoutParams lp;
View view = LayoutInflater.from(mContext).inflate(R.layout.jump_sign_manager_tip, null);
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(width, height);
int viewWidth = view.getMeasuredWidth(); // 获取宽度
int viewHeight = view.getMeasuredHeight();//获取高度
dialogWindow.setGravity(Gravity.TOP | Gravity.LEFT);
signManagerTipBar.setContentView(view);
Button btn=view.findViewById(R.id.btn_jump_sign_manager);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signManagerTipBar.dismiss();
//插入签名操作,用户可执行自定义行为
mPdfView.insertSign();
}
});
lp = dialogWindow.getAttributes();
lp.x = (int) screenX-viewWidth/2;
lp.y = (int) screenY-viewHeight/2;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
signManagerTipBar.getWindow().setAttributes(lp);
signManagerTipBar.setCanceledOnTouchOutside(true);
signManagerTipBar.setCancelable(true);
//显示用户自定义弹窗
signManagerTipBar.show();
}
};
```
**接口参数说明:**
- pressTime
类型:long
说明:设置长按时间,单位ms,当到达长按时间,监听被触发
- longPressListener
类型:LongPressListener
说明:监听函数
screenX:屏幕X轴坐标
screenY:屏幕Y轴坐标