上記サイトが一番ためになるようだ。
ただ、Layoutをxmlでやったり、Java内で動的にやったり、
なんだか関係がよくわからない、というレベルの私なので、
下記を参考にした。
おそらく一番わかり易い記事だ。
[PROCESS]
[#1 Make Project]
eclipseで新規作成メニューからAndroidプロジェクトを選択。
あなたの好きなプロジェクト名を作成する。
あなたの好きなプロジェクト名を作成する。
[#2 Configure main.xml]
これを下記に上書き。
- <linearlayout android:id="@+id/frags" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
- <listview android:id="@+id/number_list" android:layout_height="match_parent" android:layout_width="250dip">
- <fragment android:id="@+id/the_frag" android:layout_height="match_parent" android:layout_width="match_parent" class="com.bambooflower.fragmenttest.MyFragment">
- </fragment></listview></linearlayout>
この"com.bambooflower.fragmenttest"はあなたの決めたプロジェクト名に変更。
[#3 Add codes to MainActivity]
こんどは、プロジェクトを仕切るActivityを書き換え。
プロジェクト名+Activityのファイルをダブルクリック。
クラス内にコードを追加。
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ListView l = (ListView) findViewById(R.id.number_list);
- ArrayAdapter numbers = new ArrayAdapter<string>(getApplicationContext(),
- android.R.layout.simple_list_item_1,
- new String [] {
- "one", "two", "three", "four", "five", "six"
- });
- l.setAdapter(numbers);
- l.setOnItemClickListener(this);
- }
- /**
- * Add a Fragment to our stack with n Androids in it
- */
- private void stackAFragment(int nAndroids) {
- Fragment f = new MyFragment(nAndroids);
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.replace(R.id.the_frag, f);
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
- ft.addToBackStack(null);
- ft.commit();
- }
- /**
- * Called when a number gets clicked
- */
- public void onItemClick(AdapterView parent, View view, int position, long id) {
- stackAFragment(position + 1);
- }
- </string>
書き換え後はこんな感じ。
eclipse上で赤線が出てたら、依存関係が解消されていないので、
マウスオーバー(赤線の上にカーソルを合わせる)してサジェスト(カーソルの下に出てくる助言)野中の"import ****"を選択する。
[#4 Add Fragment Class]
プロジェクト名を右クリックして、クラスを追加する。
クラス名は、またあなたがつけてください。
クラス作成後のソースに下記を追加。
- private int nAndroids;
- public MyFragment() {
- }
- /**
- * Constructor for being created explicitly
- */
- public MyFragment(int nAndroids) {
- this.nAndroids = nAndroids;
- }
- /**
- * If we are being created with saved state, restore our state
- */
- @Override
- public void onCreate(Bundle saved) {
- super.onCreate(saved);
- if (null != saved) {
- nAndroids = saved.getInt("nAndroids");
- }
- }
- /**
- * Save the number of Androids to be displayed
- */
- @Override
- public void onSaveInstanceState(Bundle toSave) {
- toSave.putInt("nAndroids", nAndroids);
- }
- /**
- * Make a grid and fill it with n Androids
- */
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
- int n;
- Context c = getActivity().getApplicationContext();
- LinearLayout l = new LinearLayout(c);
- for (n = 0; n < nAndroids; n++) {
- ImageView i = new ImageView(c);
- i.setImageResource(R.drawable.gallery_thumb);
- l.addView(i);
- }
- return l;
- }
追加後はこんな感じ。
[#5 build]
ビルドしてみましょう。
Android 4.0.3の横長画面エミュレータではこんな感じ。
Galaxy Nexusではこんな画面。なんか意図したものと違う・・・。
私が作りたいのは、Galaxy Nexusでは横に並ぶのではなく、画面遷移・・
そうかListViewもFragmentにしないといけないのかな。
次回はちゃんと画面遷移ができるようにするぞー!
No comments:
Post a Comment