Activity_Fragment
package com.example.myapplication_one;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity_Fragment extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fragment);
/*
* 1.创建Fragment实例
* 2.获取FragmentManager:1、getSupportFragmentManager();2、getFragmentManager();
* 3.开启事务
* 4.添加、替换Fragment
* 5.提交事务
*/
button = (Button) findViewById(R.id.button_fragment1);
replaceFragment(new MyFragment2());//默认去加载一个MyFragment2()
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new MyFragment3());//从第二个MyFragment2()替换成MyFragment3()
}
});
}
public void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.framelayout1,fragment);//framelayout1使用的是帧布局中的id
//模拟返回栈
transaction.addToBackStack(null); //模拟返回栈,再次点击按钮的时候,返回原本的布局,但是我这里没起作用,不知道为什么。
transaction.commit();
}
}
layout_fragment.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">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#0000ff"
android:text="主页面" />
<LinearLayout
android:id="@+id/ln_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- name属性是用来寻找自己设置的页面布局的-->
<!-- 以静态的方式加载fragment -->
<fragment
android:id="@+id/fragment_1"
android:name="com.example.myapplication_one.MyFragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!--<fragment
android:id="@+id/fragment_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="com.example.myapplication_one.MyFragment2"/>-->
<!-- 帧布局 -->
<FrameLayout
android:id="@+id/framelayout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
></FrameLayout>
</LinearLayout>
</LinearLayout>
MyFragment1.java
package com.example.myapplication_one;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/*
* resourse:第一个参数是页面布局文件
* root:第二个参数,父类
* attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
*/
View view = inflater.inflate(R.layout.layout_fragment1,
container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)
//false没有保留父类容器的布局样式
return view;//返回当前的布局文件
}
}
layout_fragment1.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"
android:background="#ff0">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment1" />
<!-- android:layout_weight="1"-->
<Button
android:id="@+id/button_fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment1" />
<!-- android:layout_weight="1"-->
</LinearLayout>
MyFragment2.java
package com.example.myapplication_one;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/*
* resourse:第一个参数是页面布局文件
* root:第二个参数,父类
* attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
*/
View view = inflater.inflate(R.layout.layout_fragment2,
container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)
return view;//返回当前的布局文件
}
}
layout_fragment2.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"
android:background="#ff0000">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment2" />
<!-- android:layout_weight="1"-->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment2" />
<!-- android:layout_weight="1"-->
</LinearLayout>
MyFragment3.java
package com.example.myapplication_one;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment3 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/*
* resourse:第一个参数是页面布局文件
* root:第二个参数,父类
* attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
*/
View view = inflater.inflate(R.layout.layout_fragment3,
container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)
return view;//返回当前的布局文件
}
}
layout_fragment3.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"
android:background="#0ff">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment3" />
<!-- android:layout_weight="1"-->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment3" />
<!-- android:layout_weight="1"-->
</LinearLayout>
最终运行结果:
点击FRAGMENT1按钮: