▼Androidメモ▼
フラグメント


フラグメントを利用するプログラムを作成する。
【スマートフォン】


【タブレット】



ソースコード
FragmentEx.java
package net.npaka.fragmentex;
import android.os.Bundle;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;

//フラグメントの利用
public class FragmentEx extends Activity {
    //アクティビティ起動時に呼ばれる
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        
        //レイアウトの指定
        setContentView(R.layout.main);
    }
    
    //リストフラグメントの生成
    public static class TitlesFragment extends ListFragment {
        private int pos=-1;

        //アクティビティ生成完了時に呼ばれる
        @Override
        public void onActivityCreated(Bundle bundle) {
            super.onActivityCreated(bundle);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, 
                new String[]{"ページ0","ページ1","ページ2"}));
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            if (isTablet(getActivity())) showDetails(0);
        }

        //リスト要素クリック時に呼ばれる
        @Override
        public void onListItemClick(ListView l,View v,int pos,long id) {
            showDetails(pos);
        }

        //詳細の表示
        private void showDetails(int index) {
            Context context=getActivity().getApplication();
            
            //フラグメントの切り換え
            if (isTablet(context)) {
                getListView().setItemChecked(index,true);
                if (pos==index) return;
                DetailActivity.DetailsFragment fragment=
                    DetailActivity.DetailsFragment.newInstance(index);
                FragmentTransaction ft=getFragmentManager().beginTransaction();
                ft.replace(R.id.details,fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                pos=index;
            }
            //アクティビティの起動
            else {
                getListView().setItemChecked(index,false);
                Intent intent=new Intent(context,DetailActivity.class);
                intent.putExtra("index",index);
                getActivity().startActivity(intent);
            }
        }
    }
    
    //タブレットかどうかの取得
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout&
            Configuration.SCREENLAYOUT_SIZE_MASK)==
            Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }
}

DetailActivity.java
package net.npaka.fragmentex;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

//詳細アクティビティ
public class DetailActivity extends Activity{
    //アクティビティ生成時に呼ばれる
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        
        //インテントからのパラメータ取得
        int index=0;
        Bundle extras=getIntent().getExtras();
        if (extras!=null) index=extras.getInt("index");
        
        //レイアウトの生成
        FrameLayout layout=new FrameLayout(this);
        layout.setId(R.id.details);
        setContentView(layout);
        
        //フラグメントの配置
        DetailsFragment fragment=DetailsFragment.newInstance(index);
        FragmentTransaction ft=getFragmentManager().beginTransaction();
        ft.replace(R.id.details,fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }
    
    //詳細フラグメント
    public static class DetailsFragment extends Fragment {
        //インスタンス生成時に呼ばれる
        public static DetailsFragment newInstance(int index) {
            DetailsFragment fragment=new DetailsFragment();
            Bundle bundle=new Bundle();
            bundle.putInt("index",index);
            fragment.setArguments(bundle);
            return fragment;
        }

        //フラグメントのビュー生成時に呼ばれる
        @Override
        public View onCreateView(LayoutInflater inflater,
            ViewGroup container,Bundle bundle) {
            if (container==null) return null;
            TextView textView=new TextView(getActivity());
            textView.setText("ページ"+
                getArguments().getInt("index",0)+"の詳細");
            textView.setTextSize(24);
            return textView;
        }
    }    
}

layout/main.xml(スマートフォン用)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment 
        class="net.npaka.fragmentex.FragmentEx$TitlesFragment"
        android:id="@+id/titles" 
        android:layout_weight="1"
        android:layout_width="50px"
        android:layout_height="match_parent" />
</LinearLayout>

layout-xlarge/main.xml(タブレット用)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment 
        class="net.npaka.fragmentex.FragmentEx$TitlesFragment"
        android:id="@+id/titles" 
        android:layout_weight="1"
        android:layout_width="50px"
        android:layout_height="match_parent" />
    <FrameLayout 
        android:id="@+id/details" 
        android:layout_weight="3"
        android:layout_width="0px"
        android:layout_height="match_parent" />
</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.npaka.fragmentex"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="12" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".FragmentEx" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="DetailActivity"
            android:name=".DetailActivity" >
        </activity>
    </application>

</manifest>



−戻る−