简体   繁体   English

为Webview片段加载微调器

[英]Loading Spinner for Webview Fragment

The following is the code in my ScoreFragment.java that shows a Webview. 以下是我的ScoreFragment.java中显示Webview的代码。

IMPORT BLAH BLAH
public class ScoreFragment extends Fragment {

public ScoreFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View mainView = inflater.inflate(R.layout.fragment_score, container, false);
    WebView myWebView = (WebView) mainView.findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            return true;
        }
    });


    myWebView.loadUrl("xyz");
   return mainView;
}
}

This code is waht i used in my ACtivity for Another different project that also shows Webview , But this has a Loading spinner that shows up in the left of the action bar: 这段代码是我在ACtivity中用于另一个也显示Webview的不同项目时使用的,但这具有一个Loading旋转器,它显示在操作栏的左侧:

public class Facebook extends Activity {

WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_facebook);



       mWebView = (WebView) findViewById( R.id.MyWebview ); 
       mWebView.setWebViewClient(new WebViewClient());
       mWebView.getSettings().setJavaScriptEnabled(true);   
       mWebView.getSettings().setSupportZoom(true);       
       mWebView.getSettings().setBuiltInZoomControls(false); 
       mWebView.loadUrl("https://www.facebook.com/thebanginbeats");

       final Activity MyActivity = this;
       mWebView.setWebViewClient(new WebViewClient() {

           @Override 
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
              return false;
            }

           public void onBackPressed (){
               if (mWebView.canGoBack()) {
                    mWebView.goBack();       
                }
                else {
                    //My exit alert code goes here.    
                }
            }

           public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)     {
               handler.proceed() ;
               }

           @Override
           public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
            setProgressBarIndeterminate(true);   
            setProgressBarIndeterminateVisibility(true);
           }


           @Override
           public void onPageFinished(WebView view, String url) {
               setProgressBarIndeterminate(false);   
                setProgressBarIndeterminateVisibility(false);
            super.onPageFinished(view, url);

           }


       } );

}


}

Adding the line of code "requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);" 添加代码行“ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);” from the Activity into my Fragment doesn't work. 从活动到我的片段无效。 What code would be required to be changed here>? 在这里需要更改什么代码?

EDIT : I have changed the code in my fragment to this below. 编辑:我已经更改了下面的片段中的代码。 Also the menu.xml is under the menu directory and the item_layout.xml is under the layout directory. 另外,menu.xml在菜单目录下,而item_layout.xml在布局目录下。 There are no errors except for the line " loading.setVisible(toggle); " which throws and error asking me to set it as Boolean toggle = false; 除“ loading.setVisible(toggle);”行外没有其他错误,该行引发错误并要求我将其设置为布尔型toggle = false; Any idea as to what is causing it ? 是否知道是什么原因引起的?

And the line of code " getActivity().invalidateOptionsMenu()" should it be under onPageFinished ? 并且代码行“ getActivity()。invalidateOptionsMenu()”是否应在onPageFinished下? *Show in code below *在下面的代码中显示

public class ScoreFragment extends Fragment {

public ScoreFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View mainView = inflater.inflate(R.layout.fragment_score, container, false);
    WebView myWebView = (WebView) mainView.findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            return true;
        }

        @Override
           public void onPageFinished(WebView view, String url) {
            getActivity().invalidateOptionsMenu(); //On finishing the page loading
            super.onPageFinished(view, url);

           }

    });


    myWebView.loadUrl("xyz");
   return mainView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true); //This tells the parent activity that the Fragment wants  to add items to the Actionbar
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu, menu);
    //Inflate the menu for this fragment
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    //This is the important part here you can switch the visibility of your menu item
    MenuItem loading = menu.findItem(R.id.progress_bar);

    //Here I use a boolean that is global to my class to set the visibility of the   item.
    loading.setVisible(toggle);
}



}

I'd suggest a custom view in you action bar, I mean, make your own progress bar. 我建议您在操作栏中创建一个自定义视图,也就是说,创建自己的进度条。

Start creating an xml with the view for the progress bar, it can be just like this: 开始使用进度条的视图创建xml,就像这样:

item_layout.xml item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

Then create the menu file: 然后创建菜单文件:

menu.xml menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item
        android:id="@+id/progress_bar"
        app:showAsAction="always"
        android:title="Title"
        app:actionLayout="@layout/item_layout">
    </item>    
</menu>

As you can see we add the layout file we created as the action (or icon) for the menu item. 如您所见,我们将添加创建的布局文件作为菜单项的动作(或图标)。

Now in your Fragment class you need to specify that you want to add something to the ActionBar . 现在,在Fragment类中,您需要指定要向ActionBar添加一些内容。 Just override the onActivityCreated method and use setHasOptionsMenu 只需重写onActivityCreated方法并使用setHasOptionsMenu

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true); //This tells the parent activity that the Fragment wants to add items to the Actionbar
    }

Since you are using the Action Bar you need to override this two methods: 由于您正在使用Action Bar您需要覆盖这两种方法:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu, menu);
        //Inflate the menu for this fragment
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        //This is the important part here you can switch the visibility of your menu item
        MenuItem loading = menu.findItem(R.id.progress_bar);
        //Here I use a boolean that is global to my class to set the visibility of the item.
        loading.setVisible(toggle);
    }

Finally whenever you want to change the visibility of the item all you need to do is call 最后,只要您想更改商品的可见性,只需调用

getActivity().invalidateOptionsMenu();

This method will force the Action Bar to recreate and onPrepareOptionsMenu will be called. 此方法将强制重新创建Action Bar ,并将onPrepareOptionsMenu If you change the value of the boolean toggle you will toggle the visibility of the item as well. 如果您更改布尔值切换的值,则也将切换项目的可见性。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM