第一步:新建工程(略);
第二步:新建3个Fragment,它们是LeftFragment,RightFragment,AnotherRightFragment,让它们自动生成的XML文件。
各自的文件内容如下:
---------------------------------
LeftFragment.java 包文件让它自动导入即可
---------------------------------
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_left, container, false);
}
}
代码解説:LayoutInflater是一个抽象类,当我们平时需要加载layout文件来转换成View的场景都会用到它,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
inflater.inflate(R.layout.fragment_left, container, false)
就是将布局文件中的fragment_left实例化为一个视图。
inflate有两个参数和三个参数的重载形式,二个的实际上是执行三个参数的:
View inflate(int resource, ViewGroup root)
View inflate(int resource, ViewGroup root, boolean attachToRoot)
如果root为null或者attachToRoot为false时,则调用layout.xml中的根布局的属性并且将其作为一个View对象返回。
如果root不为null,但attachToRoot为false时,则先将layout.xml中的根布局转换为一个View对象,再调用传进来的root的布局属性设置给这个View,然后将它返回。
如果root不为null,且attachToRoot为true时,则先将layout.xml中的根布局转换为一个View对象,再将它add给root,最终再把root返回出去。(两个参数的inflate如果root不为null也是相当于这种情况)
---------------------------------------------
RightFragment.java
--------------------------------------------
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_right, container, false);
}
}
--------------------------------------------
AnotherRightFragment.java
-------------------------------------------
public class AnotherRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_another_right, container, false);
}
}
上面三个java文件,除了类名不一样之外,代码形式是一样的。
------------------------------------------
MainActivity.java
------------------------------------------
public class MainActivity extends AppCompatActivity {
public static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"这是测试");
AnotherRightFragment fragment=new AnotherRightFragment();//建替换碎片对象
FragmentManager fm=getSupportFragmentManager(); //建碎片管理对象
FragmentTransaction ft=fm.beginTransaction();//建碎片事务
ft.replace(R.id.right_layout,fragment);//替换界面中的布局,注意这里不是替换界面中的右碎片,而是替换帧布局,否则做不出效果。
ft.addToBackStack(null);//目的是为了用手机上的后退键回到原来的状态。
ft.commit(); //上面都准备好后,在这里实施真正的替换。
}
});
}
}
------------------------------------------
activity_main.xml
-------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment" <!-- 一定要给碎片一个id号,不然程序运行后一闪而过 -->
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.jintingbo.fragmenttest.LeftFragment"
tools:layout="@layout/fragment_left" />
<FrameLayout
android:id="@+id/right_layout" <!-- 一定要给它一个id号,不然程序运行后一闪而过 -->
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<fragment
android:id="@+id/right_fragment" <!-- 一定要给它一个id号,不然程序运行后一闪而过 -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.jintingbo.fragmenttest.RightFragment"
tools:layout="@layout/fragment_right" />
</FrameLayout>
</LinearLayout>
----------------------------------------------
fragment_left.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeftFragment">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
----------------------------------------------
fragment_right.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
tools:context=".RightFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
-------------------------------------------
fragment_another_right.xml
-------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
tools:context=".AnotherRightFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="中华人民共和国" />
</LinearLayout>
----------------------------------------------
来源:CSDN
作者:庭博
链接:https://blog.csdn.net/jintingbo/article/details/104745290