繁体   English   中英

为Webview片段加载微调器

[英]Loading Spinner for Webview Fragment

以下是我的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;
}
}

这段代码是我在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);

           }


       } );

}


}

添加代码行“ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);” 从活动到我的片段无效。 在这里需要更改什么代码?

编辑:我已经更改了下面的片段中的代码。 另外,menu.xml在菜单目录下,而item_layout.xml在布局目录下。 除“ loading.setVisible(toggle);”行外没有其他错误,该行引发错误并要求我将其设置为布尔型toggle = false; 是否知道是什么原因引起的?

并且代码行“ getActivity()。invalidateOptionsMenu()”是否应在onPageFinished下? *在下面的代码中显示

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);
}



}

我建议您在操作栏中创建一个自定义视图,也就是说,创建自己的进度条。

开始使用进度条的视图创建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"/>

然后创建菜单文件:

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>

如您所见,我们将添加创建的布局文件作为菜单项的动作(或图标)。

现在,在Fragment类中,您需要指定要向ActionBar添加一些内容。 只需重写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
    }

由于您正在使用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);
    }

最后,只要您想更改商品的可见性,只需调用

getActivity().invalidateOptionsMenu();

此方法将强制重新创建Action Bar ,并将onPrepareOptionsMenu 如果您更改布尔值切换的值,则也将切换项目的可见性。

希望能帮助到你!

暂无
暂无

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

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