操作栏未显示进度条

5
我有一个 WebView 并且希望在加载页面时在 ActionBar 中显示进度。该应用程序使用 AppCompat Android 库,目标和编译 SDK 版本均为 21。WebView 正常运行。
我认为问题出现在方法 setSupportProgress(int progress) 中。
我的代码如下:
Activity:
public class AuthActivity extends ActionBarActivity {

private boolean isConfirmationRequired = false;
private Utils utils = new Utils();

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_auth);

    final String authUrl = "some url";
    final WebView authWebView = (WebView) findViewById(R.id.auth_web_view);
    authWebView.getSettings().setJavaScriptEnabled(true);
    authWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setTitle(R.string.loading);
            setSupportProgress(newProgress * 100);

            if (newProgress == 100)
                setTitle(R.string.sign_in);
        }
    });

    authWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!url.equals(authUrl))
                isConfirmationRequired = true;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            boolean hasUserBrokenAuth = url.equals("some url") ||
                    url.equals("some url");
            boolean isUserAuthorized = url.startsWith("some url" +
                    "access_token=");

            if (hasUserBrokenAuth) {
                if (isConfirmationRequired)
                    utils.showConfirmDialog(AuthActivity.this);
                else
                    finish();
            } else {
                utils.webViewLoadUrlIfInternetIsConnected(AuthActivity.this, authWebView, url);
                if (isUserAuthorized)
                    saveAuthData(url);
            }
            return true;
        }
    });

    utils.webViewLoadUrlIfInternetIsConnected(this, authWebView, authUrl);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (isConfirmationRequired) {
            utils.showConfirmDialog(this);
        }
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    if (isConfirmationRequired) {
        utils.showConfirmDialog(this);
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }

}

private void saveAuthData(String url) {
    String accessToken = utils.extractPattern(url, "access_token=(.*?)&");
    String userId = utils.extractPattern(url, "user_id=(\\d*)");
}

utils.webViewLoadUrlIfInternetIsConnected:

public void webViewLoadUrlIfInternetIsConnected(final Context context, final WebView webView,
                                                final String url) {
    if (isInternetConnected(context)) {
        webView.loadUrl(url);
    } else {
        showSnackbarInternetDisconnected(context, new ActionClickListener() {
            @Override
            public void onActionClicked(Snackbar snackbar) {
                snackbar.dismiss();
                webViewLoadUrlIfInternetIsConnected(context, webView, url);
            }
        });
    }
}
2个回答

3

AppCompat不支持像以前一样显示进度视图,因此您可以从代码中删除以下内容:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminateVisibility(...);

有可能使用新的工具栏,我今天自己试了一下,但是它会使事情变得更加复杂。我最终采用的解决方案是添加一个菜单项,引用一个布局,该布局引用一个可绘制对象:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/menu_progress"
    android:visible="false"
    app:showAsAction="always"
    app:actionLayout="@layout/progress_indeterminate"
    tools:ignore="AlwaysShowAction,UnusedAttribute"/>

然后布局看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/custom_progress_wheel" />

以下是可自定义的drawable(您可以根据需要进行自定义):
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="720" >

<shape
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thicknessRatio="12"
    android:useLevel="false" >

    <gradient
        android:centerColor="#FFFFFF"
        android:centerY="0.50"
        android:endColor="#FFFFFF"
        android:startColor="#000000"
        android:type="sweep"
        android:useLevel="false" />
</shape>
</rotate>

然后在onCreateOptionsMenu中,您使用以下内容初始化menuItem:
mProgressMenu = menu.findItem(R.id.menu_progress);

使用setVisibility方法,您可以在活动的任何位置更改菜单的可见性。


1
非常好的解决方案,谢谢。只是在ProgressBar布局中,我添加了style="?android:attr/android:progressBarStyleSmall"代替自定义drawable。 - Yar
我知道,但我明确地使用了自定义的可绘制对象,以便在每个Android版本上都有相同的可绘制对象。 - David

0

您应该尝试使用setSupportProgressBarVisibility(boolean)来更新ProgressBarVisibility,方法如下:

// set Visibility to TRUE
setSupportProgressBarVisibility(true); 

然后,在onProgressChanged中,当进度结束时禁用其Visibility:

@Override
public void onProgressChanged(WebView view, int newProgress) {
    setTitle(R.string.loading);
    setSupportProgress(newProgress * 100);

    if (newProgress == 100) {
        // set it to FALSE
        setSupportProgressBarVisibility(false);
        setTitle(R.string.sign_in);
    }
    //...
}

更新:

另一种解决方案可能是使用自定义的Progress View添加到AppCompat中。

您需要填充包含水平进度条的自定义视图,获取ActionBar容器ViewGroup并将自定义视图添加到容器中:

/** Inflate a custom view **/
View ab_progressView = getLayoutInflater().inflate(R.layout.ab_progressbar, null)
/** Get the ActionBar container **/
ViewGroup ab_container = (ViewGroup) findViewById(R.id.action_bar_container);
/** Add the custom view **/
ab_container.addView(ab_progressView);

如果 ab_container 返回了一个 NullPointerException,那么你只需创建一个新的 Runnable 在一个并行线程中获取其id即可。
你需要将这个新视图设置在 ActionBar 的底部:
/** Create layout parameters **/
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
/** Set the gravity **/
params.gravity = Gravity.BOTTOM;
/** Set these params to your view **/
ab_progressView.setLayoutParams(params);

然后更新进度:

/** Get the progress bar **/
final ProgressBar progressBar = (ProgressBar) ab_progressView.findViewById(R.id.progress);

@Override
public void onProgressChanged(WebView view, int newProgress) {
    /** Update the progress **/
    progressBar.setProgress(newProgress * 100);
}

当进度结束时,只需移除自定义视图:
/** Remove the custom view added **/
ab_container.removeView(ab_progressView);

我没有测试过这个解决方案,但它应该可以工作。


不幸的是,它没有帮助到我。还有其他解决方案吗? - Alex Andro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接