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

标签 java android webview android-webview

从这个 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() 但我可以似乎弄明白了。这里有什么想法吗?

最佳答案

在布局中定义的

ViewsetContentView() 调用中实例化,实例化后不能覆盖类方法。用必要的覆盖为你的匿名实例交换布局定义的 View 是可行的,但它基本上丢弃了从布局创建的实例,并不是最干净的方法。

我会建议另一种方法。在您的 FlingView 类中创建一个 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();
        }
    }
);

关于java - 如何使用自定义 webview 和覆盖功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40095318/

相关文章:

javascript - Android WebView 编译表单并使用 Javascript 提交

java - 无法加载类 'org.gradle.api.internal.component.Usage'

java - 使用 Flying Saucer 和 iText 问题的 XHTML 到 PDF

java - Android 获取 facebook 电子邮件地址

html - 使我的移动 html 全屏

ios - webview 在 swift 3 中打开 native fb 应用程序?

java - Android Studio codestyles/Project.xml 配置

java - 带有正则表达式的Android拆分字符串不起作用

java - 从 C 而不是 Java 管理 Android 应用程序设置?

android - 使用相机后,我的 android 应用程序中的 onActivityResult 出现 NullPointerException 和 Failure Delivering Result 异常