如何使用自定义webview和重写函数



反问这个问题:

我做了一个名为FlingView的自定义webview,它包含手势事件。上述问题中的策略对我来说很有效;我的活动布局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();
        }
    }
);

如果你想用刚刚创建的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

最新更新