我创建了一个具有滚动侦听器和触摸侦听器的自定义 web 视图。我正在其中加载一个 HTML 代码,为此我使用了 javascript,它使用for Loop创建img
标签。
String htnlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><script type=\"text/javascript\">for(a=1;a<=10;a++)document.write('<img style=\"border-style:dotted;border-width:10px;border-color:black;\"src=\"http://shiaislamicbooks.com/books_snaps/EN183/'+a+'.jpg\" alt=\"Page Not Found\"/>');</script></body></html>";
wv.loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);
现在我想获取img
webview 的标签 onTouchListner 的位置。webview中是否有任何方法可以返回<img>
标签的位置。
整个代码
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.webkit.WebView;
public class scorllableWebview extends WebView {
private OnScrollChangedCallback mOnScrollChangedCallback;
public scorllableWebview(final Context context) {
super(context);
}
public scorllableWebview(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public scorllableWebview(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(final int l, final int t, final int oldl,
final int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedCallback != null)
mOnScrollChangedCallback.onScroll(l, t);
}
public OnScrollChangedCallback getOnScrollChangedCallback() {
return mOnScrollChangedCallback;
}
public void setOnScrollChangedCallback(
final OnScrollChangedCallback onScrollChangedCallback) {
mOnScrollChangedCallback = onScrollChangedCallback;
}
/**
* Impliment in the activity/fragment/view that you want to listen to the
* webview
*/
public static interface OnScrollChangedCallback {
public void onScroll(int l, int t);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("onTouchEvent", event.getAction() + "");
// I want to get the position of <img> tag here.
// what is the method which return the <img> tag position
return super.onTouchEvent(event);
}
}
提前致谢。