上一篇我们讲解了基本的fragmen使用,这一章节我们讲解如何动态加载fragment。下一节我们讲解的是fragment的生命周期。
本示例通过一个fragment启动另一个fragment。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button butotn = findViewById(R.id.butotn);
butotn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmenRight());
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl, fragment);
fragmentTransaction.commit();
}
}
对应的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="horizontal"
tools:context="com.example.administrator.myfragment2.MainActivity">
<fragment
android:id="@+id/lift"
android:name="com.example.administrator.myfragment2.FragmentLift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
FragmentLift:
public class FragmentLift extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lift, container, false);
return view;
}
}
对应的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:id="@+id/butotn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="左侧"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
</android.support.constraint.ConstraintLayout>
FragmenRight:
public class FragmenRight extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
return view;
}
}
对应的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="右侧"
android:textColor="#000000"/>
</android.support.constraint.ConstraintLayout>
addToBackStack(null)的使用
通过调用 addToBackStack(null),可以将替换事务保存到返回栈,以便用户能够通过按返回按钮撤消事务并回退到上一片段。
在MainActivity中:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button butotn = findViewById(R.id.butotn);
Button button = findViewById(R.id.button);
butotn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmenRight());
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment2(new FragmenRight());
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl, fragment);
fragmentTransaction.commit();
}
private void replaceFragment2(Fragment fragment) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
对应的xml:
同上
FragmentLift:
同上
FragmentLift对应的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:id="@+id/butotn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="左侧"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"/>
</android.support.constraint.ConstraintLayout>
转发表明出处:https://blog.csdn.net/qq_35698774/article/details/106426102
感谢:https://developer.android.google.cn/guide/components/fragments
android互助群: