0

i want android webview data binding in mvvm pattern

my code (view model)

  companion object {
        @JvmStatic
        @BindingAdapter("loadUrl")
        fun WebView.setUrl(url: String) {
            this.loadUrl(url)
        }
    }

xml

<WebView
                android:id="@+id/webViewTerm"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:loadUrl="@{signUpViewModel.url}"
                />

The binding was successful,

but I want to add an extra header , I don't know how to put the data I want in that object bound to the companion object

Could you give me a little hint for me?

tomerpacific
  • 4,704
  • 13
  • 34
  • 52

1 Answers1

0

You can pass extra headers to your WebView tag. See the example below.

In your XML, add headers to your XML tag:

<WebView
    android:id="@+id/webViewTerm"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:loadUrl="@{signUpViewModel.url}"
    app:headers="@{signUpViewModel.headers}"
    />

In your code:

 @JvmStatic
 @BindingAdapter(value = ["app:URL","app:headers"], requireAll = true)
 fun loadUrl(view: WebView, url:String?, extraHeaders:Map<String, String> )
  {
     view.loadUrl(url,extraHeaders)
  }
Alpha 1
  • 4,118
  • 2
  • 17
  • 23
  • Cannot find a setter for that accepts parameter type 'java.util.HashMap' If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches. I'm sorry, but I don't know what to do when I get this error Could you help me a little more? –  May 14 '20 at 09:55
  • The binding adapter is taken from the viewbindingAdapter, and in singinupViewmodel, it is only mutablelivedata and livedata. by connect xml –  May 15 '20 at 01:46
  • I solved this problem. Thanks for your advice, thank you. –  May 21 '20 at 05:27