为Webview片段加载微调器

时间:2014-07-24 20:41:25

标签: java android eclipse android-fragments 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;
}
}

此代码是我在我的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);”从Activity到我的片段不起作用。这里需要更改哪些代码>?

编辑:我已将我的片段中的代码更改为以下内容。 menu.xml也位于菜单目录下,而item_layout.xml位于布局目录下。 除了行“load.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);
}



}

1 个答案:

答案 0 :(得分:0)

我建议您在操作栏中设置自定义视图,我的意思是,制作自己的进度条。

开始使用进度条的视图创建一个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将被调用。 如果更改布尔值切换的值,您还将切换项目的可见性。

希望它有所帮助!