监听WebView的Scroll滑动效果——下滑隐藏actionBar,下滑显示actionBar

一:自定义WebView,添加回调的监听器

package app.coolwhether.com.zhihudailynews.support;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

/**
 * Created by kirito on 2017.02.12.
 */

public class ScrollWebView extends WebView {
    private static final String TAG = "ScrollWebView";
    public OnScrollListener listener;

    public ScrollWebView(Context context) {
        this(context,null);
    }

    public ScrollWebView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScrollWebView(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 (listener != null){
            if (t - oldt <= 2){
                listener.onScrollDown();
            }
            if(oldt - t >= 2) {
                listener.onScrollUp();
            }
        }
    }

    public void setListener(OnScrollListener listener){
        this.listener = listener;
    }

    public interface OnScrollListener{
        void onScrollUp();//上滑
        void onScrollDown();//下滑
    }
}

二:布局文件使用自定义的WebView


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <app.coolwhether.com.zhihudailynews.support.ScrollWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
LinearLayout>

三:引用该ScrollWebView

mWebView.setListener(new ScrollWebView.OnScrollListener() {
            @Override
            public void onScrollUp() {
                getSupportActionBar().show();
            }

            @Override
            public void onScrollDown() {
                getSupportActionBar().hide();
            }


        });

监听WebView的Scroll滑动效果——下滑隐藏actionBar,下滑显示actionBar_第1张图片

你可能感兴趣的:(webView,ScrollView)