我有:
- 仅显示给定 URL 的 WebView 的 WebViewFragment。
- 一个 TabbedWebViewHandler 处理创建包含 WebViewFragments 的选项卡并将它们添加到操作栏。
- 一个 HelpActivity,它创建并显示 4 个 WebViewFragment(分别用于“更多信息”、“条款和条件”、“信用”和“此应用程序的目的”帮助屏幕)。
所有这一切都很好,除了第一个选项卡在 HelpActivity 首次启动时始终只是一个空白的黑屏:
其他选项卡工作正常,如果选择另一个选项卡然后重新选择“信息”选项卡,第一个选项卡(在本例中为“信息”选项卡)将正确呈现其 Web 视图。
在创建第一个选项卡并将其添加到操作栏选项卡后,我总是选择actionBar.selectTab(newTab);
. 我知道执行此操作的代码正在运行,因为日志包含“选择第一个选项卡”。
我还将 TabbedWebViewHandler 用于执行相同操作的其他活动(包括只有一个“选项卡”且不显示选项卡导航的活动),因此我更愿意修复 TabbedWebViewHandler 而不是解决方法在帮助活动中。
如果相关的话,我正在使用 ActionBarSherlock / Android 支持库来提供我的选项卡功能。
如何确保我的活动的第一个选项卡始终正确显示?
WebViewFragment
public class WebViewFragment extends Fragment {
private static final String TAG = WebViewFragment.class.getSimpleName();
private String url;
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, String.format("onCreateView(): URL is '%s'", url));
View v = inflater.inflate(R.layout.snippet_webview, container, false);
WebView wv = (WebView) v.findViewById(R.id.webViewWebView);
wv.loadUrl(url);
return v;
}
public void setUrl(String url) {
this.url = url;
}
}
选项卡式WebViewHandler
public class TabbedWebViewHandler {
private static final String TAG = "TabbedWebViewHandler";
private final ActionBar actionBar;
private final Context hostContext;
private final FragmentManager fragmentManager;
public TabbedWebViewHandler(SherlockFragmentActivity host) {
this.hostContext = (Context) host;
this.actionBar = host.getSupportActionBar();
this.fragmentManager = host.getSupportFragmentManager();
}
public void addTab(String title, String renderUrl) {
Tab newTab = makeTab(title, renderUrl);
actionBar.addTab(newTab);
// FIXME: buggy! tabs don't show on first load
if (actionBar.getTabCount() == 1) {
/* first tab: select it by default */
Log.d(TAG, "Selecting first tab");
actionBar.selectTab(newTab);
}
if (actionBar.getTabCount() > 1) {
/* more than one tab: enable navigating between tabs */
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
}
private Tab makeTab(String title, String renderUrl) {
WebViewFragment f =
(WebViewFragment)
SherlockFragment.instantiate(
hostContext,
WebViewFragment.class.getName());
f.setUrl(renderUrl);
ActionBar.TabListener l = new WebViewFragmentTabListener(f);
Tab newTab = actionBar.newTab();
newTab.setText(title);
newTab.setTabListener(l);
return newTab;
}
private class WebViewFragmentTabListener implements ActionBar.TabListener {
private final WebViewFragment fragment;
public WebViewFragmentTabListener(WebViewFragment f) {
this.fragment = f;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// replace current fragment with this fragment
if (ft == null) {
fragmentManager
.beginTransaction()
.replace(android.R.id.content, fragment)
.commit();
} else {
ft.replace(android.R.id.content, fragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
}
帮助活动
public class HelpActivity extends BaseActivity {
private static final String TAG = "HelpActivity";
private static final String FURTHER_URL =
"file:///android_asset/further_info.html";
private static final String DISCLAIMER_URL =
"file:///android_asset/terms_and_conditions.html";
private static final String CREDITS_URL =
"file:///android_asset/image_credits.html";
private static final String PURPOSE_URL =
"file:///android_asset/purpose.html";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate()");
super.onCreate(savedInstanceState);
TabbedWebViewHandler twvh = new TabbedWebViewHandler(this);
twvh.addTab("Info", FURTHER_URL);
twvh.addTab("Terms", DISCLAIMER_URL);
twvh.addTab("Credits", CREDITS_URL);
twvh.addTab("Purpose", PURPOSE_URL);
}
}
日志输出
I/HelpActivity(20038): onCreate()
D/TabbedWebViewHandler(20038): Selecting first tab
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8