上一篇写了:使用<fragment>标签静态加载Fragment,继续把动态加载也写完。
下图的绿色部分就是一个Fragment:
核心代码:
ShowNameFragment mFragment = ShowNameFragment.newInstance("zj", "hubei");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =manager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout,mFragment);
fragmentTransaction.commit();
//简写
ShowNameFragment mFragment = ShowNameFragment.newInstance("zj", "hubei");
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout, mFragment)
.commit();
具体实现:
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowNameFragment mFragment = ShowNameFragment.newInstance("zj", "hubei");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =manager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout,mFragment);
fragmentTransaction.commit();
}
}
注意添加id
<FrameLayout 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:id="@+id/framelayout"
tools:context=".MainActivity">
FrameLayout>
注意:newInstance()是Android studio默认添加的
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ShowNameFragment extends Fragment {
private static final String NAME = "name";
private static final String PROVINCE = "province";
private String mName;
private String mProvince;
public ShowNameFragment() {
}
public static ShowNameFragment newInstance(String param1, String param2) {
ShowNameFragment fragment = new ShowNameFragment();
Bundle args = new Bundle();
args.putString(NAME, param1);
args.putString(PROVINCE, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mName = getArguments().getString(NAME);
mProvince = getArguments().getString(PROVINCE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_show_name, container, false);
}
<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"
android:background="#00ff00"
tools:context=".ShowNameFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:layout_gravity="center"
android:textColor="#ffffff"
android:background="#ff0000"/>
FrameLayout>
试下:
//activity 布局修改
//fragment布局宽高改为200dp
//加载id修改为linearlayout
getSupportFragmentManager().beginTransaction()
.replace(R.id.linearlayout, mFragment)
.commit();
正常运行:
当然不是。尝试在LinearLayout布局里面再加一个Fragment,这次不用replace(),这次用add()
//activity修改
ShowNameFragment mFragment = ShowNameFragment.newInstance("zj", "hubei");
ShowNameFragment mFragment2 = ShowNameFragment.newInstance("zj", "guangdong");
getSupportFragmentManager().beginTransaction()
.replace(R.id.linearlayout, mFragment)
.commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.linearlayout, mFragment2)
.commit();
//fragment修改
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_show_name, container, false);
TextView tv=view.findViewById(R.id.tv_name);
tv.setText(mProvince);
return view;
}
效果如下图:
所以一个布局里面是可以加载多个Fragment的。
因为多数情况下,一个布局里面只需要显示一个Fragment。这种情况下比起LinearLayout和RelativeLayout,使用FrameLayout更好。