3 回答
TA贡献1804条经验 获得超3个赞
就像是:
public class ObservableWebView extends WebView
{
private OnScrollChangedCallback mOnScrollChangedCallback;
public ObservableWebView(final Context context)
{
super(context);
}
public ObservableWebView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public ObservableWebView(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, oldl, oldt);
}
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, int oldl, int oldt);
}
}
应该可以使用,但未经测试,但是可以在Android中的几乎所有其他视图上使用。
您将实现如下:
wv = (ObservableWebView) findViewById(R.id.scorllableWebview);
wv.setOnScrollChangedCallback(new OnScrollChangedCallback(){
public void onScroll(int l, int t, int oldl, int oldt){
if(t> oldt){
//Do stuff
System.out.println("Swipe UP");
//Do stuff
}
else if(t< oldt){
System.out.println("Swipe Down");
}
Log.d(TAG,"We Scrolled etc...");
}
});
TA贡献1828条经验 获得超4个赞
从API 23开始,您不再需要这样做,您只需 在所有视图(包括WebView)上使用新的OnScrollChangeListener即可。但是由于您仍然需要支持旧版本,因此仍然可以使用@ Chris.Jenkins的建议。我对提议的类进行了一些调整,使其与新的OnScrollChangeListener接口“更加兼容”:
public class ObservableWebView extends WebView {
private OnScrollChangeListener onScrollChangeListener;
public ObservableWebView(Context context) {
super(context);
}
public ObservableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollChangeListener != null) {
onScrollChangeListener.onScrollChange(this, l, t, oldl, oldt);
}
}
public void setOnScrollChangeListener(OnScrollChangeListener onScrollChangeListener) {
this.onScrollChangeListener = onScrollChangeListener;
}
public OnScrollChangeListener getOnScrollChangeListener() {
return onScrollChangeListener;
}
public interface OnScrollChangeListener {
/**
* Called when the scroll position of a view changes.
*
* @param v The view whose scroll position has changed.
* @param scrollX Current horizontal scroll origin.
* @param scrollY Current vertical scroll origin.
* @param oldScrollX Previous horizontal scroll origin.
* @param oldScrollY Previous vertical scroll origin.
*/
void onScrollChange(WebView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY);
}
}
TA贡献1775条经验 获得超11个赞
这是公认的答案的更优雅的Kotlin版本
class ObservableWebView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {
var scrollListener: WebViewScrollListener? = null
override fun onScrollChanged(scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
super.onScrollChanged(scrollX, scrollY, oldScrollX, oldScrollY)
when {
scrollY > oldScrollY -> scrollListener?.onScrollDown()
scrollY < oldScrollY -> scrollListener?.onScrollUp()
}
scrollListener?.onScroll(scrollX, scrollY, oldScrollX, oldScrollY)
}
}
interface WebViewScrollListener{
fun onScroll(scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int)
fun onScrollDown()
fun onScrollUp()
}
- 3 回答
- 0 关注
- 734 浏览
添加回答
举报