所以我试图在加载和活动开始时为menuitem
刷新设置动画。webview
我也试图在menuitem
没有必要的时候隐藏前锋。以下代码可以完美运行,除非 webview 正在加载并且您回击或尝试一次执行多个任务(单击大量历史记录)。每当发生这种情况时,刷新menuitem
往往会复制、动画和重叠自身。这也发生在前锋身上menuitem
。它往往与动画刷新图标重叠。有什么办法可以防止这种情况发生吗?以下代码完全是我的代码,除了不相关的内容已被修剪。有人可以帮我克服这个错误吗?
public class TideWeb extends Activity {
private static WebView webView;
String name = null;
MenuItem refresh, forward;
ActionBar ab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent launchingIntent = getIntent();
name = launchingIntent.getType().toString();
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.WebView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
if (refresh != null && refresh.getActionView() == null) {
StartAnimation();
}
ab.setSubtitle("Loading...");
webView.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
setSupportProgress(progress * 100);
if (progress == 100) {
view.clearCache(true);
StopAnimation();
ab.setSubtitle(name);
}
}
});
String url = launchingIntent.getData().toString();
webView.loadUrl(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.tideweb, menu);
forward = menu.findItem(R.id.forward_menu);
if (webView.isFocused() && webView.canGoForward()) {
forward.setVisible(true);
} else {
forward.setVisible(false);
}
ab.setSubtitle("Loading...");
refresh = menu.findItem(R.id.refresh_menu);
if (refresh != null && refresh.getActionView() == null) {
StartAnimation();
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.forward_menu:
webView.goForward();
invalidateOptionsMenu();
break;
case R.id.refresh_menu:
webView.reload();
invalidateOptionsMenu();
break;
case android.R.id.home:
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return true;
}
public void onBackPressed() {
if (webView.isFocused() && webView.canGoBack()) {
webView.goBack();
invalidateOptionsMenu();
} else {
super.onBackPressed();
finish();
}
}
private void StartAnimation() {
if (refresh != null && refresh.getActionView() == null) {
final LayoutInflater inflater = (LayoutInflater) getApplication()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout ivRefresh = (RelativeLayout) inflater.inflate(
R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(
getApplication(), R.anim.rotate);
ivRefresh.startAnimation(rotation);
refresh.setActionView(ivRefresh);
}
}
private void StopAnimation() {
if (refresh != null && refresh.getActionView() != null) {
refresh.getActionView().clearAnimation();
refresh.setActionView(null);
}
}
}
加载,然后返回:
加载,然后来回: