フラジャーズまとめ

6388 ワード

一、全体工程図:
Fragment总结
二、main.xml
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:baselineAligned="false"

    android:orientation="vertical" >



    <fragment

        android:id="@+id/fragment1"

        android:name="com.jltxgcy.fragmentdemo.Fragment1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1" />



   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/main_layout"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:baselineAligned="false"

    android:layout_weight="1" >



	</LinearLayout>

    <TextView 

        android:id="@+id/tv_display"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>



</LinearLayout>
三、fragment 1.xml
 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#00ff00" >

    <TextView

        android:id="@+id/tv_fragment1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="This is fragment 1"

        android:textColor="#000000"

        android:textSize="25sp" />



</LinearLayout>
四、fragment 2.xml
 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#ffff00" >



    <TextView

        android:id="@+id/tv_fragment2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="This is fragment 2"

        android:textColor="#000000"

        android:textSize="25sp" />



</LinearLayout>
五、MainActivity.java
 
 
package com.jltxgcy.fragmentdemo;



import android.os.Bundle;

import android.app.Activity;

import android.support.v4.app.FragmentActivity;

import android.view.Menu;



public class MainActivity extends FragmentActivity {

	public static final String TAG = "Fragment2";

	

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Fragment2 fragment2 = new Fragment2();  

        getSupportFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();

    }

    

    public void testMainActivity(){

    	Fragment1 fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);

		fragment1.testFragment1();

    }

    

    public void testFindviewById(){

    	findViewById(R.id.tv_display);

    	findViewById(R.id.tv_fragment2);

    }

}
六、フラジャバ
 
 
package com.jltxgcy.fragmentdemo;



import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



public class Fragment1 extends Fragment {

	

	@Override

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fragment1, container, false);

	}

	

	public void testFragment1(){

		Log.d("jltxgcy", "testFragment1");

	}

}
 
七、フラジャメント2.java
 
package com.jltxgcy.fragmentdemo;



import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



public class Fragment2 extends Fragment {

	private ViewGroup mViewGroup;



	@Override

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mViewGroup = container;

		testFragment2();

		return inflater.inflate(R.layout.fragment2, container, false);

	}

	

	public void testFragment2(){

		MainActivity mainActivity = (MainActivity)getActivity();

		mainActivity.testMainActivity();

		Fragment1 fragment1 = (Fragment1) getFragmentManager().findFragmentById(R.id.fragment1);

		fragment1.testFragment1();

	}

	

	public void testFindviewById(){

		mViewGroup.findViewById(R.id.tv_fragment2);

		getActivity().findViewById(R.id.tv_display);

		getActivity().findViewById(R.id.tv_fragment1);

	}



}
八、上記では、それぞれ動的および静的にフラジャイティをロードする過程を紹介しました.
 
        Activityで自分のコントロールViewを取得する:findView ById(R.id.tvudisplay);
        Activityでは、FragmentコントロールView:findView ById(R.id.tvfragment 2)を取得します.
        Activityではフラジャイティ方法を使用します.
        Fragment 1 fragment 1=(Frangement 1)get Support Frangement Manager().findFrangement ById(R.id.frangement 1) 
        fragment 1.testFragment 1()
        Fragmentで自分のコントロールViewを取得する:mView Group.findView ById(R.id.tvfragment 2);
        FragmentでActivityコントロールView:get Activity().findView ById(R.id.tvudisplay);
        Fragmentでは、他のコントロールからFragmentのView:get Activity().findView ById(R.id.tvfragment 1);
        FragmentでActivityを使用する方法:
        MainActivity manActivity=(MainActivity)getActivity()        manActivity.testMainActivity()
        Fragmentでは他のフラジャイムの方法を使用します.
        Fragment 1 fragment 1=(Frangent 1)get FrangemenntManager()findFrangement ById(R.id.frangement 1);        fragment 1.testFragment 1()
九、プログラム運転の最終結果は:
         
十、コードは後でgithubに伝えます.