MainActivity
package com.example.demo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class MainActivity extends FragmentActivity {
public static String temp="weesdf";
public static String getTemp() {
return temp;
}
public static void setTemp(String temp) {
MainActivity.temp = temp;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent intent=new Intent(MainActivity.this,);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FirstFragment first = new FirstFragment();
//如果用add()的话会出现不完全覆盖效果
ft.replace(R.id.container, first);
ft.commit();
}
//切换到第二个fragment
public void onClick_btn(View v){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SecondFragment second = new SecondFragment();
ft.replace(R.id.container, second);
ft.commit();
}
}
Activity_Second
package com.example.demo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class Activity_Second extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
FirstFragment
package com.example.demo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
Button bt_first = (Button) view.findViewById(R.id.bt_first);
bt_first.setText(MainActivity.temp);
bt_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(getActivity(),Activity_Second.class);
startActivity(intent);
}
});
return view;
}
}
SecondFragment
package com.example.demo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragment_second, container, false);
Button bt_second = (Button) view.findViewById(R.id.bt_second);
bt_second.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.move_in, R.animator.move_out);
FirstFragment first = new FirstFragment();
ft.replace(R.id.container, first);
ft.commit();
}
});
return view;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick_btn"
android:text="@string/app_name" />
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="200dp" />
</LinearLayout>
fragmentfirst.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#cba" >
<TextView
android:id="@+id/tv_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="这是第一个Fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bt_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="点我进入下一个" />
</LinearLayout>
fragmentsecond.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#abc"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="这是第二个Fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bt_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="点我回到第一个" />
</LinearLayout>
已在资源中上传demo源码http://download.csdn.net/detail/djun100/5974251