2016年7月24日日曜日

Visual Studio 2015 with Xamarin AndroidでWebViewにProgressBarを追加する

Point XML

RelativeLayoutでwebviewを先に置いて、ProgressBarで上書き


Point source

SetWebChromeClientにoverrideしたプライベートクラスを適応し、
そのクラスにProgressBarを渡しているだけ


気になること
ProgressBarを渡したけど、問題は出ないのだろうか?
こういうのを確かめないからいつまでもコピペ。

追記
2016/07/25
問題でてた。タップされるまで動画が表示されない。
いったん修正。

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/DIframeLayout">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/FwebView"
        android:layout_centerInParent="true" />
  <ProgressBar
    android:id="@+id/FprogressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</RelativeLayout>

ソース

//…省略…/
public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Bundle savedInstanceState)
        {
            var root = inflater.Inflate(Resource.Layout.fragmentWebView, container, false);
            fWebView = root.FindViewById<WebView>(Resource.Id.FwebView);
            fProgress = root.FindViewById<ProgressBar>(Resource.Id.FprogressBar);

//…省略…/
 fWebView.SetWebChromeClient(new myWebChromeClient(fProgress));

//…省略…/
return root;

//…省略…/

 public class myWebChromeClient : WebChromeClient
        {
            private ProgressBar fProgress = null;
            public myWebChromeClient(ProgressBar setProgress) : base()
            {
                this.fProgress = setProgress;
            }

            public override void OnProgressChanged(WebView view, int newProgress)
            {
                if (fProgress != null)
                {
                    fProgress.Progress = newProgress;
                    if (newProgress == 100)
                    {
                     fProgress.Animate().SetDuration(1000).Alpha(0.0f);
                    }
                    else
                    {
                        fProgress.Alpha = 1.0f;
                    }
              }
          }

1 件のコメント: