Android开发——动态功能(四)fragment详解(二)点击替换

版权声明:如转载请表明出处 https://blog.csdn.net/weixin_42247720/article/details/89472709

fragment详解(二)

上一篇写到了把fragment在activity上显示

这一篇写点击按钮实现fragment的替换

示例代码

在上篇基础上添加

fragmentB的java文件

package com.example.administrator.exercise.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.exercise.R;

public class FragmentB extends android.app.Fragment {

    private TextView tv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        tv = view.findViewById(R.id.fm_b_tv);
    }
}

fragmentB的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_fragment_a"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.administrator.exercise.fragment.FragmentA">
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="我是Bfragment"
    android:textSize="16sp"
    android:gravity="center"
    android:id="@+id/fm_b_tv"/>
</RelativeLayout>

activity的java文件

package com.example.administrator.exercise.fragment;

        import android.app.Fragment;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;

        import com.example.administrator.exercise.R;

public class ContainerActivity extends AppCompatActivity {

    Button bt_jump;

    private FragmentA Afragment;
    private FragmentB Bfragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        bt_jump = (Button) findViewById(R.id.container_bt);
        //给按钮设置监听
        bt_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Bfragment == null){
                    Bfragment = new FragmentB();
                }
                    getFragmentManager().beginTransaction().replace(R.id.container_fl,Bfragment).commitAllowingStateLoss(); //调用替换方法
            }
        });
        //实例化Afragment
        Afragment = new FragmentA();
        //把Afragment添加到Activity中,记得调用commit
        getFragmentManager().beginTransaction().add(R.id.container_fl,Afragment).commitAllowingStateLoss();

    }
}

跳转不了的可能原因:
你的FragmentB的布局文件的RelativeLayout里添加的padding,小编因为paddingTop没删,死活跳转不了

运行结果图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42247720/article/details/89472709
今日推荐