私は持っている:
- 特定の URL の WebView を表示するだけの WebViewFragment。
- WebViewFragments を含むタブの作成とアクション バーへの追加を処理する TabbedWebViewHandler。
- 4 つの WebViewFragments を作成して表示する HelpActivity (それぞれ「詳細情報」、「利用規約」、「クレジット」、「このアプリの目的」のヘルプ画面用)。
HelpActivity が最初に開始されたときに最初のタブが常に空白の黒い画面であることを除いて、これはすべて正常に機能します。
他のタブは問題なく動作し、最初のタブ (この場合は「情報」タブ) は、別のタブが選択されてから「情報」タブが再度選択された場合、Web ビューを適切にレンダリングします。
最初のタブを作成した後、常に最初のタブを選択し、それを でアクション バーのタブに追加しますactionBar.selectTab(newTab);
。ログに「最初のタブの選択」が含まれているため、これを行うコードが実行されていることがわかります。
また、同じことを行う他のアクティビティ (「タブ」が 1 つしかなく、タブ ナビゲーションが表示されないアクティビティを含む) にも TabbedWebViewHandler を使用しているため、回避策を講じるよりも TabbedWebViewHandler を修正することをお勧めします。 HelpActivity で。
関連する場合は、ActionBarSherlock / Android サポート ライブラリを使用してタブ機能を提供しています。
アクティビティの最初のタブが常に正しく表示されるようにするにはどうすればよいですか?
WebView フラグメント
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;
}
}
TabbedWebViewHandler
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