如何使用自定义 webview 和覆盖功能?

How to use custom webview AND override functions?

反弹这个 question:

我制作了一个名为 FlingView 的自定义 webview,其中包含手势事件。上面链接问题中的策略对我来说很好用;我的 activity 布局 xml 没问题:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/coordinator_layout_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="net.brianclements.android.WebViewActivity">

        <net.brianclements.android.FlingView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:keepScreenOn="true" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_webview_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_webview_drawer_left" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_webview_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_webview_drawer_right" />

</android.support.v4.widget.DrawerLayout>

onCreate() 这样做:

setContentView(R.layout.activity_web_view);

我用这个方法成功加载了它:

mWebview = (FlingView) findViewById(R.id.webview);

现在,问题是当我尝试覆盖其中的函数时:

    mWebview = new FlingView(this, null) {
        @Override
        public void onLeftFling() {
            Log.d(TAG, ACTIVITY_TAG + "onFling left: ");
            someDynamicObject.thatCantBeUsedStatically();
        }

        @Override
        public void onRightFling() {
            Log.d(TAG, ACTIVITY_TAG + "onFling right: ");
            anotherDynamicObject.thatCantBeUsedStatically();
        }
    };

那么我现在如何将 mWebview 插入回布局中?第一种铸造方法使它变得如此简单。但根据我的研究,我相信它现在涉及充气机的某种组合,findViewById()addView()removeView(),但我似乎无法弄清楚。有什么想法吗?

如果您想用刚创建的 FlingView 替换当前的 FlingView,您需要执行以下操作:

CoordinatorLayout cl = (CoordinatorLayout) findViewById(R.id.coordinator_layout_webview);
cl.removeViewAt(0); // removing previous flingview
cl.addView(mWebView, 0); // adding the new inflated one

View 在布局中定义的方法在 setContentView() 调用中实例化,实例化后无法覆盖 class 方法。使用必要的覆盖为您的匿名实例交换布局定义的 View 是可行的,但它实际上是丢弃从布局创建的实例,并不是最干净的方法。

我会建议另一种方法。在您的 FlingView class 中创建一个 interface,可用于将这些操作反馈给您的 Activity;与 OnClickListener.

非常相似

例如:

public class FlingView extends WebView {

    public interface FlingListener {
        void onLeftFling();
        void onRightFling();
    }

    private FlingListener mListener;

    public void setFlingListener(FlingListener listener) {
        mListener = listener;
    }

    public void onLeftFling() {
        if (mListener != null) {
            mListener.onLeftFling();
        }
    }

    public void onRightFling() {
        if (mListener != null) {
            mListener.onRightFling();
        }
    }

    ...
}

在您的 Activity 中,从展开的布局中找到您的 FlingView 实例后,只需在其上设置一个侦听器实例即可。

setContentView(R.layout.activity_web_view);
...

mWebview = (FlingView) findViewById(R.id.webview);

mWebview.setFlingListener(new FlingView.FlingListener() {
        @Override
        public void onLeftFling() {
            Log.d(TAG, ACTIVITY_TAG + "onFling left: ");
            someDynamicObject.thatCantBeUsedStatically();
        }

        @Override
        public void onRightFling() {
            Log.d(TAG, ACTIVITY_TAG + "onFling right: ");
            anotherDynamicObject.thatCantBeUsedStatically();
        }
    }
);