問題の起きる環境というか条件
Eclipse で新しい Android Application Project を作って、その際に Minimum Required SDK を API 11 にして Navigation Type を Fixed Tabs + Swipe にする。
すると、タプが 3つあってスワイプでそれぞれを変更できるアプリケーションができあがる。
ここで、MainActivity を少し書き換えて 2番目のタブには 自前の Fragment を用意、そこに WebView を貼り付ける。
MainActivity の変更箇所は SectionsPagerAdapter の getItem の部分。
2 番目のタブに自前の Fragment(MyWebFragment)を用意しているだけ。
@Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. Fragment fragment; if (position==1) { fragment = new MyWebFragment(); } else { fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); } return fragment; }
で、自前の Fragment(MyWebFragment)はこんな感じ。
Fragment を拡張して WebView を貼り付けているだけ。
public class MyWebFragment extends Fragment { WebView mWebView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.webview_fragment, container, false); mWebView = (WebView) v.findViewById(R.id.myWebView); mWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String urlString) { return false; } }); String url = "http://jp.msn.com/"; mWebView.loadUrl(url); return v; } }
で、これを起動するとちゃんと 2番目のタブで指定した URL が表示される。
のだけど、リンクをタップしても見た目が反応しない。
上の画面だと「ニュース」の下の項目(花火会場で爆発など)をタップしてもテキストが反転しない。
ただし、少しスクロールさせた後だと、ちゃんとテキストが反転してその後は常に問題がなくなる。
また、ログにはエラーメッセージが表示されている。
08-14 17:07:41.784: E/webcoreglue(4786): Should not happen: no rect-based-test nodes found
どうやら起きちゃいけないことが起きているらしい…。
というのが自分のハマった問題点なのだけど、Web 上では特に誰も言及していないところを見ると自分の実装の仕方がいけないんだろうな〜と思ったりもする…。
更に、この問題はどの Web サイトでも必ず起きるわけではないみたい。
普通に表示されるページもあったりするのでやっかい。
ただ、WebView を貼るのが 1番目のタブだと問題がない。
2 番目のタブの Fragment は、最初に 1番目のタブが表示された時にほぼ同時に実体化?していて、裏で WebView の中身も読み込まれてる。
どうもこれが悪さしているみたいなんだけど…。
ってか、Fragment に WebView 貼り付けるような場合は WebViewFragment を使えってのが正しい道なんだろうな〜。