Eclipse4.2だと、プロジェクト作成の時点でいくつかのフォームから初期フォームを選択することができます。
ここでは、WebViewを使って、あらかじめ複数のサイトを開いて、順次見ていくというアプリを作ります。
動作確認バージョンはICECREAM SANDWICH(4.0.3)で行います。
Ctrl+F11を押し、実行モード「Androidアプリケーション」を選択して実行。正常動作を確認します。
1. 元のサンプルはTextViewで文字を表示するだけです。ここを書き換えます。
変更前-------------------------------
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
変更後-------------------------------
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)<3){
/// ※1
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}else{
View rootView = inflater.inflate(R.layout.webview, container, false);
WebView wv = (WebView) rootView.findViewById(R.id.webview);
wv.loadUrl("http://www.google.co.jp");
return rootView;
}
}
}
onCreateViewというメソッドを書き換えています。
オリジナルは、
インデックス番号を取得して、その番号を表示する
というものなので、変更後は
if文でインデックス番号をチェックし、3番(初期コードでは3画面分になっているため)になったら処理を変える
ようにしています。ここで、webview.xmlを作成しています。
→ ファイル → 新規 → その他 → Android XMLレイアウト・ファイル → RelativeLayoutを選択
し、ファイル名を「webview.xml」とします。
これで、ページを3ページ目に切り替えた時、Googleのホームページが表示されるようになります。