Webview不会重新加载

时间:2015-04-21 10:28:20

标签: java android android-fragments webview android-studio

我正在使用片段并在操作栏上添加了一个项目来刷新名为 airport_menuRefresh 的webview,但是当我点击它时没有任何反应。我有3个标签。我尝试了不同的方法,但它们都不起作用。 这是我的1tab的代码(tab1仅包含要刷新的无效逻辑)。

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;    
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class GamesFragment extends Fragment {

private ProgressBar progress;
private WebView myWebView;



public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String url = "http://google.com";
    int delay = 6 * 1000;
    View rootView = inflater.inflate(R.layout.fragment_games, container, false);
    myWebView = (WebView)rootView.findViewById(R.id.webViewGames);
    myWebView.setWebChromeClient(new myWebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    progress = (ProgressBar)rootView.findViewById(R.id.progressBar);
    progress.setMax(100);

    Handler handler = new Handler();
    MyRunnable runnable = new MyRunnable(url, myWebView);
    handler.postDelayed(runnable, delay);

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView View, String url) {
            GamesFragment.this.progress.setProgress(0);
            View.loadUrl(url);
            return true;
        }



    });



    myWebView.setOnKeyListener(new android.view.View.OnKeyListener() {
        @Override
         public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            WebView webView = (WebView) v;

            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                    break;
            }
        }
        return false;
        }
    });



   return rootView;

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
           case R.id.airport_menuRefresh:
            GamesFragment.this.myWebView.loadUrl("http://google.com");
            return true;
    }
    return super.onOptionsItemSelected(item);
}




public class MyRunnable implements Runnable {

     String url;
    private WebView mywebview;

    public MyRunnable(String url, WebView wv) {
        this.url = url;

        this.mywebview = wv;
    }

    public void run() {

        mywebview.loadUrl(url);

    }
}



private class myWebViewClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        GamesFragment.this.setValue(newProgress);
        super.onProgressChanged(view, newProgress);
    }
}




public void setValue(int progress) {
    this.progress.setProgress(progress);
}


}

这是我的主要活动:

 import android.annotation.TargetApi;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.view.ViewPager;
 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;

 import com.tech.example.adaptor.SlidingTabLayout;
 import com.tech.example.adaptor.TabsPagerAdapter;

 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 public class MainActivity extends ActionBarActivity {
 SlidingTabLayout tabs;
 ViewPager viewPager;
 TabsPagerAdapter adapter;
 Toolbar toolbar;
 private Menu optionsMenu;

CharSequence Titles[]={ "tab1", "tab2", "tab3" };
int Numboftabs =3;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    adapter = new TabsPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
    // Assigning ViewPager View and setting the adapter
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);
    setRefreshActionButtonState(true);
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }

        @Override
        public int getDividerColor(int i) {
            return 0;
        }
    });

    tabs.setViewPager(viewPager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    this.optionsMenu = menu;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}


public void setRefreshActionButtonState(final boolean refreshing) {
    if (optionsMenu != null) {
        final MenuItem refreshItem = optionsMenu
                .findItem(R.id.airport_menuRefresh);
        if (refreshItem != null) {
            if (refreshing) {
                refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
            } else {
                refreshItem.setActionView(null);
            }
        }
    }
}

}

修改

我编辑了我的主要活动:

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.airport_menuRefresh:

            GamesFragment.myWebView1.loadUrl("http://example.com");

            MoviesFragment.myWebView.loadUrl("http://example2.com");

            TopRatedFragment.myWebView2.loadUrl("http://example3.com");
            return true;
    }
    return super.onOptionsItemSelected(item);
    }

现在它正在重新加载,但假设我在tab1上点击重新加载所有3个标签重新加载!任何解决方案?

2 个答案:

答案 0 :(得分:0)

我猜错误是在您方法的WebView名称中。只要您导入:

import android.view.View;

在这个方法中

public boolean shouldOverrideUrlLoading(WebView View, String url) {
//                                              ^-----> UPPERCASE

你打这个电话:

View.loadUrl(url);

您正在尝试调用View的静态方法,而不是方法签名中的View实例。

试试这个:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                                              ^---> lowercase
    GamesFragment.this.progress.setProgress(0);
    view.loadUrl(url);
//  ^---> lowercase
    return true;
}

答案 1 :(得分:0)

最后我找到了解决方案。

将onCreateOptionsMenu和onOptionsItemSelected从主要活动移动到这样的片段标签

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   .............
  .............
 .............
setHasOptionsMenu(true);
.................
.................
 }


@Override
public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // handle item selection
    switch (item.getItemId()) {
        case R.id.airport_menuRefresh:

              TopRatedFragment.this.myWebView2.loadUrl("http://myexample.com");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

这就像一个魅力。