5

我有一个片段活动,左侧有一个 ListFragment,右侧有一个带有 WebView 的片段。该功能工作正常,但我想在网页完成加载时显示一个显示“正在加载...”的进度对话框。我怎样才能完成这个看似微不足道的任务?

下面是我的代码:

public class ArticleListActivity extends FragmentActivity implements
    ArticleListFragment.OnArticleSelectedListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articlelist_fragment);
    }

    public void onArticleSelected(String contentLink) {
        ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.articleview_fragment);

        if (viewer == null || !viewer.isInLayout()) {
            Intent showContent = new Intent(getApplicationContext(),
                    ArticleViewerActivity.class);

            showContent.setData(Uri.parse(contentLink));
            startActivity(showContent);
        } else {
            viewer.updateUrl(contentLink);
        }
    }
}

这是我的 FragmentActivity

public class ArticleViewerActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articleview_fragment);

        Intent launchingIntent = getIntent();
        String content = launchingIntent.getData().toString();

        ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.articleview_fragment);

        viewer.updateUrl(content);
    }
}

这是我的片段

public class ArticleViewerFragment extends Fragment {

    private ProgressDialog progressBar; 

    private WebView viewer = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        viewer = (WebView) inflater.inflate(R.layout.webview_layout, container, false);
        viewer.loadUrl("file:///android_asset/default.html");
        return viewer;
    }

    public void updateUrl(String newUrl) {
        if (viewer != null) {
            viewer.loadUrl(newUrl);
        }
    }
}
4

1 回答 1

1

Use this code:

final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);
pd.setCancelable(false);

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }
});
webview.loadUrl("file:///android_asset/default.html");
于 2012-08-26T08:24:20.593 回答