我们在实际的应用中,页面之间的切换,如果使用Activity的话,太沉重了,这个时候,Fragment就是一个不错的选择。
接下来我们用一个例子来实现它:通过一个页面启动另一个页面,并且可以返回到之前的页面。
- 先创建一个Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
//这里加载的是MainFragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
}
}`
- 这里我们创建main_activity.xml,他的id是container。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
- 创建MainFragment
public class MainFragment extends Fragment {
public static MainFragment newInstance() {
return new MainFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
rootView.findViewById(R.id.btnShowAnotherFragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getParentFragmentManager().beginTransaction()
.addToBackStack(null) //表示允许后退,不至于直接退出Activity,显得生硬
.replace(R.id.container, new AnotherFragment())
.commit();
}
});
return rootView;
}
}
- main_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.main.MainFragment">
<Button
android:id="@+id/btnShowAnotherFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="呈现另一个Fragment" />
</LinearLayout>
- AnotherFragment
public class AnotherFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_another,container,false);
root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getParentFragmentManager().popBackStack();
}
});
return root;
}
}
- fragment_another.xml
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是另一个fragment" />
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="后退" />
</LinearLayout>