首页 > 其他 > 详细

4.3动态切换Fragment

时间:2021-09-07 15:29:19      阅读:8      评论:0      收藏:0      [点我收藏+]

首先是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="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="replace"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="change"/>

<!--    为了动态设置,添加一个FrameLayout-->
    <FrameLayout
        android:id="@+id/fragmentLayout"
        android:background="#ff00ff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>


</LinearLayout>

Fragment.xml没啥东西

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Fragment.class其实也没有东西

package com.example.myfragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    public Fragment1() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

MainActivity.class

package com.example.myfragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button btn1;
    Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        //btn1事件监听
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击这个按钮,把Fragment1替换到FrameLayout上
               replaceFragment(new Fragment1());
            }
        });
        //btn2事件监听
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    //动态切换fragment
    private void replaceFragment(Fragment fragment) {
        //获取FragmentManger管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        //获取Fragment的触发器
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //Fragment的替换动作由fragmentTransaction完成
        fragmentTransaction.replace(R.id.fragmentLayout,fragment);
        //提交这个事件
        fragmentTransaction.commit();
    }

}

效果图如下:

技术分享图片

4.3动态切换Fragment

原文:https://www.cnblogs.com/tuyaojiao/p/15237278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!