0

所以我试图在加载和活动开始时为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);
        }
    }
}

加载,然后返回: 加载,然后返回

加载,然后来回: 加载,然后来回

4

1 回答 1

0

我并没有真正找到解决此问题的方法,但是我没有使用图像并对其进行动画处理,而是将操作视图设置为进度条。重叠不再发生,所以我想它现在已经修复了。

于 2013-07-28T03:01:29.360 回答