返回列表 發帖
Fragment1:
  1. package com.example.student.myapplication;

  2. import android.app.Fragment;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. public class Fragment1 extends Fragment{

  10.     Button btn1,btn2;
  11.     MainActivity activity;

  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         activity= (MainActivity) getActivity();
  16.     }

  17.     @Override
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  19.         getFragmentManager().popBackStack();
  20.         View view=inflater.inflate(R.layout.fragment1,container,false);

  21.         btn1= (Button) view.findViewById(R.id.button1);
  22.         btn2= (Button) view.findViewById(R.id.button2);

  23.         btn1.setOnClickListener(new View.OnClickListener() {
  24.             @Override
  25.             public void onClick(View v) {
  26.                 Fragment2 f2 = new Fragment2();
  27.                 //FragmentManager fm=getFragmentManager();
  28.                 FragmentTransaction ft =getFragmentManager().beginTransaction();
  29.                 if(activity.findViewById(R.id.frame2)!=null)
  30.                 {
  31.                     ft.add(R.id.frame2, f2, null);
  32.                 }else{
  33.                     ft.add(R.id.frame1, f2);
  34.                     ft.addToBackStack(null);
  35.                 }
  36.                 ft.commit();
  37.             }
  38.         });

  39.         btn2.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View v) {
  42.                 Fragment3 f3 = new Fragment3();
  43.                 FragmentTransaction ft =getFragmentManager().beginTransaction();
  44.                 if(activity.findViewById(R.id.frame2)!=null)
  45.                 {
  46.                     ft.add(R.id.frame2, f3, null);
  47.                 }else{
  48.                     ft.add(R.id.frame1, f3);
  49.                     ft.addToBackStack(null);
  50.                 }
  51.                 ft.commit();
  52.             }
  53.         });
  54.         return view;
  55.     }
  56. }
複製代碼
Fragment2:
  1. package com.example.student.myapplication;

  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment2 extends Fragment {

  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  10.         View view=inflater.inflate(R.layout.fragment2,container,false);
  11.         return view;
  12.     }
  13. }
複製代碼
Fragment3:
  1. package com.example.student.myapplication;

  2. import android.app.Activity;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.app.Fragment;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;

  14. public class Fragment3 extends Fragment {

  15.     @Override
  16.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  17.         View view = inflater.inflate(R.layout.fragment3, container, false);
  18.         return view;
  19.     }
  20. }
複製代碼
fragment1:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#b3e1ff">

  6.     <Button
  7.         android:layout_width="match_parent"
  8.         android:layout_height="61dp"
  9.         android:text="Go to Fragment2"
  10.         android:id="@+id/button1"
  11.         android:layout_gravity="center_horizontal"
  12.         android:textAllCaps="false"
  13.         android:textSize="24sp" />
  14.     <Button
  15.         android:layout_width="match_parent"
  16.         android:layout_height="61dp"
  17.         android:text="Go to Fragment3"
  18.         android:id="@+id/button2"
  19.         android:layout_gravity="center_horizontal"
  20.         android:textAllCaps="false"
  21.         android:textSize="24sp" />

  22.     <TextView
  23.         android:layout_width="match_parent"
  24.         android:layout_height="match_parent"
  25.         android:text="Fragment1"
  26.         android:id="@+id/textView"
  27.         android:layout_gravity="center_horizontal"
  28.         android:gravity="center"
  29.         android:textSize="28sp"
  30.         android:textColor="@android:color/black"
  31.         android:visibility="visible" />

  32. </LinearLayout>
複製代碼
fragment2:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#fddef6">

  6.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="Fragment2"
  10.         android:id="@+id/textView"
  11.         android:layout_gravity="center_horizontal"
  12.         android:gravity="center"
  13.         android:textSize="28sp"
  14.         android:textColor="@android:color/black"
  15.         android:visibility="visible" />
  16. </LinearLayout>
複製代碼
fragment:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#cbffba">

  6.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="Fragment3"
  10.         android:id="@+id/textView"
  11.         android:layout_gravity="center_horizontal"
  12.         android:gravity="center"
  13.         android:textSize="28sp"
  14.         android:textColor="@android:color/black"
  15.         android:visibility="visible" />
  16. </LinearLayout>
複製代碼
activity_main:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity"
  6.     android:orientation="vertical">

  7.     <FrameLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="0dp"
  10.         android:layout_weight="1"
  11.         android:id="@+id/frame1"
  12.         android:visibility="visible"
  13.         android:orientation="horizontal"></FrameLayout>
  14. </LinearLayout>
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity"
  6.     android:baselineAligned="false">

  7.     <FrameLayout
  8.         android:layout_width="0dp"
  9.         android:layout_height="match_parent"
  10.         android:layout_gravity="center_vertical"
  11.         android:layout_weight="2"
  12.         android:id="@+id/frame1"></FrameLayout>

  13.     <FrameLayout
  14.         android:layout_width="0dp"
  15.         android:layout_height="match_parent"
  16.         android:layout_gravity="center_vertical"
  17.         android:layout_weight="3"
  18.         android:id="@+id/frame2"></FrameLayout>
  19. </LinearLayout>
複製代碼
activity_main(land):
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity"
  6.     android:baselineAligned="false">

  7.     <FrameLayout
  8.         android:layout_width="0dp"
  9.         android:layout_height="match_parent"
  10.         android:layout_gravity="center_vertical"
  11.         android:layout_weight="1.79"
  12.         android:id="@+id/frame1"></FrameLayout>

  13.     <FrameLayout
  14.         android:layout_width="0dp"
  15.         android:layout_height="match_parent"
  16.         android:layout_gravity="center_vertical"
  17.         android:layout_weight="3"
  18.         android:id="@+id/frame2"></FrameLayout>
  19. </LinearLayout>
複製代碼

TOP

返回列表