5.分别添加两个子级Fragment,FirstFragment.java和SecondFragment.java,代码分别如下:
FirstFragment.java
1 package com.example.testdemo;
2
3 import android.os.Bundle;
4 import android.support.annotation.Nullable;
5 import android.support.v4.app.FragmentManager;
6 import android.support.v4.app.FragmentTransaction;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12
13 public class FirstFragment extends BackHandledFragment {
14 private View myView;
15 private Button btnSecond;
16
17 @Override
18 public View onCreateView(LayoutInflater inflater,
19 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
20 myView = inflater.inflate(R.layout.fragment_first, null);
21 initView();
22 return myView;
23 }
24
25 private void initView() {
26 btnSecond = (Button) myView.findViewById(R.id.btnSecond);
27 btnSecond.setOnClickListener(new OnClickListener() {
28
29 @Override
30 public void onClick(View v) {
31 SecondFragment second = new SecondFragment();
32 FragmentManager fm = getFragmentManager();
33 FragmentTransaction ft = fm.beginTransaction();
34 ft.replace(R.id.firstFragment, second);
35 ft.addToBackStack("tag");
36 ft.commit();
37 }
38 });
39 }
40
41 @Override
42 protected boolean onBackPressed() {
43 return false;
44 }
45
46 }
SecondFragment.java
1 package com.example.testdemo;
2
3 import android.os.Bundle;
4 import android.support.annotation.Nullable;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.view.ViewGroup;
8
9 public class SecondFragment extends BackHandledFragment {
10
11 private View mView;
12
13 @Override
14 public View onCreateView(LayoutInflater inflater,
15 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
16 mView = inflater.inflate(R.layout.fragment_second, null);
17 return mView;
18 }
19
20 @Override
21 protected boolean onBackPressed() {
22 return false;
23 }
24
25 }
6.三个布局文件代码如下:
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:layout_centerInParent="true"
11 android:text="FragmentActivity 父界面"
12 android:textSize="26sp" />
13
14 <Button
15 android:id="@+id/btnSecond"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_alignParentBottom="true"
19 android:text="跳转到FirstFragment" />
20
21 <FrameLayout
22 android:id="@+id/firstFragment"
23 android:layout_width="match_parent"
24 android:layout_height="match_parent" >
25 </FrameLayout>
26
27 </RelativeLayout>
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FirstFragment"
android:textColor="#000000"
android:textSize="26sp" />
<Button
android:id="@+id/btnSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="打开SecondFragment" />
</RelativeLayout>
fragment_second.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="#e5e5e5"
6 android:orientation="vertical" >
7
8 <TextView
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:layout_centerInParent="true"
12 android:text="SecondFragment"
13 android:textColor="#000000"
14 android:textSize="26sp" />
15
16 </RelativeLayout>
7.最后奉上实例链接: http://files.cnblogs.com/_ymw/TestDemo
|