2016-10-17 22 views
1

反彈此question如何使用自定義webview和覆蓋功能?

我已經做了一個自定義webview稱爲FlingView合併手勢事件。上述鏈接問題中的策略對我來說很好,我的活動佈局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()但我似乎無法弄清楚。這裏的任何想法?

回答

1

View秒,你不能實例化後覆蓋類方法。爲您的匿名實例替換佈局定義的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(); 
     } 
    } 
); 
+1

這是優秀的;它教會了我如何正確實現一個聽衆。謝謝! – brianclements

1

如果你想用您剛剛創建的當前FlingView你需要做到以下幾點:在你的佈局中定義的setContentView()呼叫被實例化

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

我把那些三行我'mWebview'創建後,並留下了一個全白的活動在我的側面導航面板和底部導航欄仍然工作,但沒有視圖內容是可見的,沒有手勢都被拾起。我可以從日誌中看到它確實已經加載到了後臺,儘管它應該如此,但它只是不可見。 – brianclements

+1

基於你說的我認爲這個問題可能是視圖加載的webview有0高度和0寬度,所以在那裏,但你看不到它。嘗試將加載的webview的高度和寬度設置爲match_parent。 –

+0

這回答了我的問題。設置'mWebview.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));'是缺少的一塊。然而,我確實使用了另一種方法,因爲它是一個更好設計的解決方案。謝謝,@Mauro。 – brianclements