安卓开发——FragMent详解

在这里插入图片描述
点击更换Fragment的布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
   >
    <Button
        android:id="@+id/btn_change"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="更换Fragment"/>

    <FrameLayout
        android:id="@+id/f1_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btn_change"/>

</RelativeLayout>

package com.example.zzm.helloworld.fragment;

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

import com.example.zzm.helloworld.R;

/**
 * Created by ZZM on 2022/2/8.
 */
public class AFragment extends Fragment {

    private TextView mTvTitle;

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle=(TextView)view.findViewById(R.id.tv_title);
    }
}

package com.example.zzm.helloworld.fragment;

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

import com.example.zzm.helloworld.R;

/**
 * Created by ZZM on 2022/2/8.
 */
public class BFragment extends Fragment {
    private TextView mTvTitle;

    @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) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle=(TextView)view.findViewById(R.id.tv_title);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="AFragment"
        android:gravity="center"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="BFragment"
        android:gravity="center"/>
</LinearLayout>

主要代码

package com.example.zzm.helloworld.fragment;

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

import com.example.zzm.helloworld.R;

public class ContainerActivity extends ActionBarActivity {

    private AFragment aFragment;
    //点击更换AFragment
    private BFragment bFragment;
    private Button mBtnChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        mBtnChange=(Button)findViewById(R.id.btn_change);
        mBtnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bFragment == null) {
                    bFragment = new BFragment();
                }
                getFragmentManager().beginTransaction().replace(R.id.f1_container,bFragment).commitAllowingStateLoss();
            }
        });

        //实例化AFragment
        aFragment=new AFragment();
        //把Afragment添加早Activity中,记得调用commit 将aFragment添加到f1_container,
        getFragmentManager().beginTransaction().add(R.id.f1_container,aFragment).commitAllowingStateLoss();




    }
}

在这里插入图片描述
在这里插入图片描述

  • Fragment中getActivity()为null的问题
  if(getActivity()!=null){
            
        }else{
            
        }
  • 向Fragment传递参数
 //向Fragment传递参数
    public static AFragment newInstance(String title){
        AFragment fragment=new AFragment();
        Bundle bundle=new Bundle();
        bundle.putString("Title",title);
        fragment.setArguments(bundle);
        return fragment;

    }

调用方法并传参

获取参数‘

 if(getArguments()!=null){
            mTvTitle.setText(getArguments().getString("title"));
        }
  • Fragment回退栈
getFragmentManager().beginTransaction().replace(R.id.f1_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
  • Fragment和Activity的通信
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值