Fragment(碎片)的替换

第一步:新建工程(略);

第二步:新建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>

----------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庭博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值