欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android 3.0 Fragment Example,androidfragment

来源: 开发者 投稿于  被查看 44861 次 评论:184

Android 3.0 Fragment Example,androidfragment


使用平台:ANDROID 3.0+

1.[文件] FragmentTestActivity.java~2KB 下载(66)

package com.example.fragmenttest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentTestActivity extends Activity implements OnItemClickListener {
    /** Called when the activity is first created. */
    @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 TestFragment(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);
	}
}

2.[文件] TestFragment.java~1KB 下载(41)

package com.example.fragmenttest;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class TestFragment extends Fragment {
    private int nAndroids;
    
    public TestFragment() {
    	
    }

   /**
    * Constructor for being created explicitly
    */
   public TestFragment(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.android);
        	l.addView(i);
        }
        return l;
    }
}

3.[文件] AndroidManifest.xml~829B 下载(40)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.fragmenttest"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="11"/>

    <application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black"
    android:hardwareAccelerated="true"
    android:debuggable="true"
    >
        <activity android:name=".FragmentTestActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

4.[文件] main.xml~627B 下载(43)

<?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"
    android:id="@+id/frags">

    <ListView
            android:id="@+id/number_list"
            android:layout_width="250dip"
            android:layout_height="match_parent" />

    <fragment class="com.example.fragmenttest.TestFragment"
            android:id="@+id/the_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</LinearLayout>

5.[图片] device-2011-07-11-091113.png

6.[图片] device-2011-07-11-091132.png

7.[图片] device-2011-07-11-091137.png

用户评论