fragment分为动态加载和静态加载
静态加载的方法
布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<span style="color:#ff0000;"> <RadioButton
android:id="@+id/first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_pressed"
android:button="@null"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:text="静态加载" /></span>
<RadioButton
android:id="@+id/second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_pressed"
android:button="@null"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:text="动态加载" />
<RadioButton
android:id="@+id/thrid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_pressed"
android:button="@null"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:text="生命周期" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_pressed"
android:button="@null"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:text="传值通信" />
</RadioGroup>
</RelativeLayout>
对应Java文件
MainActivity.java
package com.example.android_fragment;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements OnCheckedChangeListener
{
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) findViewById(R.id.radiogroup);
group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.first: {
Intent intent=new Intent(this,MainActivity2.class);
startActivity(intent);
break;
}
case R.id.second: {
MyFragment2 fragment2=new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.frame, fragment2);
beginTransaction.addToBackStack(null);
beginTransaction.commit();
break;
}
case R.id.thrid: {
Intent intent=new Intent(MainActivity.this,MainActivity3.class);
startActivity(intent);
break;
}
case R.id.fourth: {
Intent intent=new Intent(MainActivity.this,MainActivity4.class);
startActivity(intent);
break;
}
}
}
}
关系图
MainActivity2
package com.example.android_fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity2 extends Activity{
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button button=(Button) findViewById(R.id.button);
tv=(TextView) findViewById(R.id.text);
button.setText("改变");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("TextView改变了");
}
});
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.android_fragment.MyFragment"
/>
</LinearLayout>
package com.example.android_fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment{
private String aaa;
public String getAaa() {
return aaa;
}
public void setAaa(String aaa) {
this.aaa = aaa;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//layout布局文件转换成View对象
/**
* resource:Fragment需要加载的布局文件
* root:加载layout的父ViewGroup
* attactToRoot:false,不返回父ViewGroup
*/
View view = inflater.inflate(R.layout.fragment, container, false);
TextView text=(TextView) view.findViewById(R.id.text);
Button button=(Button) view.findViewById(R.id.button);
text.setText("静态加载Fragment");
button.setText("获取内容");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String value = getAaa();
Toast.makeText(getActivity(), "value="+value, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>