点击更换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的通信