我使用两个webViews在DialogFragment中的两个标签内显示。我观察到的奇怪行为是,最初,第一个标签打开为空白(在webview中没有任何内容)。
当我按下第二个选项卡时,它会正确显示静态页面,然后如果我切换回第一个选项卡,它甚至会正确显示第一个选项卡静态html。我很难搞清楚为什么它最初没有加载第一个标签。有什么想法吗?
package in.co.madhur.dashclockfeedlyextension;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LocalActivityManager;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class AboutDialog extends DialogFragment
{
private WebView aboutWebView, whatsNewView;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View v = LayoutInflater.from(getActivity()).inflate(R.layout.about_dialog, null);
builder.setView(v);
TabHost tabHost = (TabHost) v.findViewById(R.id.about_tab);
aboutWebView = (WebView) v.findViewById(R.id.about_webview);
whatsNewView = (WebView) v.findViewById(R.id.whatsnew_webview);
aboutWebView.setWebViewClient(new MyWebViewClient());
tabHost.setup();
builder.setNeutralButton(android.R.string.ok, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
builder.setPositiveButton(R.string.feedback_button, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", Consts.MY_EMAIL, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.feedback_button));
startActivity(Intent.createChooser(emailIntent, getString(R.string.send_email)));
}
});
builder.setNegativeButton(R.string.rate_button, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Uri uri = Uri.parse("market://details?id="
+ getActivity().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try
{
startActivity(goToMarket);
}
catch (ActivityNotFoundException e)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="
+ getActivity().getPackageName())));
}
}
});
TabSpec aboutTab = tabHost.newTabSpec(Consts.ABOUT_TAG);
aboutTab.setIndicator(getString(R.string.action_about));
aboutTab.setContent(R.id.tab1);
//aboutTab.setContent(intent);
TabSpec whatsnewTab = tabHost.newTabSpec(Consts.WHATS_NEW_TAG);
whatsnewTab.setIndicator(getString(R.string.whatsnew_tab));
whatsnewTab.setContent(R.id.tab2);
tabHost.addTab(aboutTab);
tabHost.addTab(whatsnewTab);
tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
Log.v(App.TAG, tabId);
}
});
aboutWebView.loadUrl(Consts.ABOUT_URL);
whatsNewView.loadUrl(Consts.CHANGES_URL);
return builder.create();
}
private class MyWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
boolean result = false;
/* ... */
// Return false to proceed loading page, true to interrupt loading
return result;
}
}
}
答案 0 :(得分:0)
在创建并初始化对话框(和WebView)后,在onViewCreated
方法中覆盖并执行加载逻辑。