如何在Android中为WebView制作滚动侦听器

如何WebView在Android中实现Scroll Listener


我尝试了这个,但它没有调用Log.i滚动Webview。


package com.example.webview.full.width;

import android.content.Context;

import android.util.AttributeSet;

import android.util.Log;

import android.webkit.WebView;

import android.widget.AbsListView;

import android.widget.AbsListView.OnScrollListener;


public class scorllableWebview extends WebView implements OnScrollListener {



Context ctx;

AttributeSet atrs;


public scorllableWebview(Context context) {

    super(context);


    ctx = context;

}


public scorllableWebview(Context context, AttributeSet atters){

    super(context, atters);


    ctx = context;

    atrs = atters;

}


@Override

public void onScroll(AbsListView view, int firstVisibleItem,

        int visibleItemCount, int totalItemCount) {


    Log.i("onScroll", "Called");

}


@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {



    Log.i("onScrollStateChanged", "Called");


}

}

这是我的 MainActivity.java


package com.example.webview.full.width;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.view.Menu;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Toast;


public class MainActivity extends Activity {


ProgressDialog progressDialog;

scorllableWebview wv;


@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    wv = (scorllableWebview) findViewById(R.id.scorllableWebview);


    wv.getSettings().setJavaScriptEnabled(true);


    wv.getSettings().setBuiltInZoomControls(true);

    wv.getSettings().supportZoom();


    progressDialog = ProgressDialog.show(MainActivity.this,

            "Loading Book...!", "Please Wait");

    progressDialog.setCancelable(true);

    });



HUWWW
浏览 863回答 3
3回答

狐的传说

就像是:public class ObservableWebView extends WebView{&nbsp; &nbsp; private OnScrollChangedCallback mOnScrollChangedCallback;&nbsp; &nbsp; public ObservableWebView(final Context context)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public ObservableWebView(final Context context, final AttributeSet attrs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super.onScrollChanged(l, t, oldl, oldt);&nbsp; &nbsp; &nbsp; &nbsp; if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t, oldl, oldt);&nbsp; &nbsp; }&nbsp; &nbsp; public OnScrollChangedCallback getOnScrollChangedCallback()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return mOnScrollChangedCallback;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mOnScrollChangedCallback = onScrollChangedCallback;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Impliment in the activity/fragment/view that you want to listen to the webview&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static interface OnScrollChangedCallback&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void onScroll(int l, int t, int oldl, int oldt);&nbsp; &nbsp; }}应该可以使用,但未经测试,但是可以在Android中的几乎所有其他视图上使用。您将实现如下:wv = (ObservableWebView) findViewById(R.id.scorllableWebview);wv.setOnScrollChangedCallback(new OnScrollChangedCallback(){&nbsp; &nbsp; public void onScroll(int l, int t, int oldl, int oldt){&nbsp; &nbsp; &nbsp; &nbsp; if(t> oldt){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Do stuff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Swipe UP");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Do stuff&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(t< oldt){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Swipe Down");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG,"We Scrolled etc...");&nbsp; &nbsp; }});

明月笑刀无情

从API 23开始,您不再需要这样做,您只需 在所有视图(包括WebView)上使用新的OnScrollChangeListener即可。但是由于您仍然需要支持旧版本,因此仍然可以使用@ Chris.Jenkins的建议。我对提议的类进行了一些调整,使其与新的OnScrollChangeListener接口“更加兼容”:public class ObservableWebView extends WebView {private OnScrollChangeListener onScrollChangeListener;public ObservableWebView(Context context) {&nbsp; &nbsp; super(context);}public ObservableWebView(Context context, AttributeSet attrs) {&nbsp; &nbsp; super(context, attrs);}public ObservableWebView(Context context, AttributeSet attrs, int defStyleAttr) {&nbsp; &nbsp; super(context, attrs, defStyleAttr);}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {&nbsp; &nbsp; super.onScrollChanged(l, t, oldl, oldt);&nbsp; &nbsp; if (onScrollChangeListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; onScrollChangeListener.onScrollChange(this, l, t, oldl, oldt);&nbsp; &nbsp; }}public void setOnScrollChangeListener(OnScrollChangeListener onScrollChangeListener) {&nbsp; &nbsp; this.onScrollChangeListener = onScrollChangeListener;}public OnScrollChangeListener getOnScrollChangeListener() {&nbsp; &nbsp; return onScrollChangeListener;}public interface OnScrollChangeListener {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Called when the scroll position of a view changes.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param v&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The view whose scroll position has changed.&nbsp; &nbsp; &nbsp;* @param scrollX&nbsp; &nbsp; Current horizontal scroll origin.&nbsp; &nbsp; &nbsp;* @param scrollY&nbsp; &nbsp; Current vertical scroll origin.&nbsp; &nbsp; &nbsp;* @param oldScrollX Previous horizontal scroll origin.&nbsp; &nbsp; &nbsp;* @param oldScrollY Previous vertical scroll origin.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; void onScrollChange(WebView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY);}}

繁星淼淼

这是公认的答案的更优雅的Kotlin版本class ObservableWebView @JvmOverloads constructor(&nbsp; &nbsp; &nbsp; &nbsp; context: Context,&nbsp; &nbsp; &nbsp; &nbsp; attrs: AttributeSet? = null,&nbsp; &nbsp; &nbsp; &nbsp; defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {&nbsp; &nbsp; var scrollListener: WebViewScrollListener? = null&nbsp; &nbsp; override fun onScrollChanged(scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {&nbsp; &nbsp; &nbsp; &nbsp; super.onScrollChanged(scrollX, scrollY, oldScrollX, oldScrollY)&nbsp; &nbsp; &nbsp; &nbsp; when {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollY > oldScrollY -> scrollListener?.onScrollDown()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollY < oldScrollY -> scrollListener?.onScrollUp()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; scrollListener?.onScroll(scrollX, scrollY, oldScrollX, oldScrollY)&nbsp; &nbsp; }}interface WebViewScrollListener{&nbsp; &nbsp; fun onScroll(scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int)&nbsp; &nbsp; fun onScrollDown()&nbsp; &nbsp; fun onScrollUp()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android