Fragment1:- package com.example.student.myapplication;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- public class Fragment1 extends Fragment{
- Button btn1,btn2;
- MainActivity activity;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- activity= (MainActivity) getActivity();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- getFragmentManager().popBackStack();
- View view=inflater.inflate(R.layout.fragment1,container,false);
- btn1= (Button) view.findViewById(R.id.button1);
- btn2= (Button) view.findViewById(R.id.button2);
- btn1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment2 f2 = new Fragment2();
- //FragmentManager fm=getFragmentManager();
- FragmentTransaction ft =getFragmentManager().beginTransaction();
- if(activity.findViewById(R.id.frame2)!=null)
- {
- ft.add(R.id.frame2, f2, null);
- }else{
- ft.add(R.id.frame1, f2);
- ft.addToBackStack(null);
- }
- ft.commit();
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment3 f3 = new Fragment3();
- FragmentTransaction ft =getFragmentManager().beginTransaction();
- if(activity.findViewById(R.id.frame2)!=null)
- {
- ft.add(R.id.frame2, f3, null);
- }else{
- ft.add(R.id.frame1, f3);
- ft.addToBackStack(null);
- }
- ft.commit();
- }
- });
- return view;
- }
- }
複製代碼 Fragment2:- package com.example.student.myapplication;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment2 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view=inflater.inflate(R.layout.fragment2,container,false);
- return view;
- }
- }
複製代碼 Fragment3:- package com.example.student.myapplication;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment3 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment3, container, false);
- return view;
- }
- }
複製代碼 fragment1:- <?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"
- android:background="#b3e1ff">
- <Button
- android:layout_width="match_parent"
- android:layout_height="61dp"
- android:text="Go to Fragment2"
- android:id="@+id/button1"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="24sp" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="61dp"
- android:text="Go to Fragment3"
- android:id="@+id/button2"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="24sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="Fragment1"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 fragment2:- <?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"
- android:background="#fddef6">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="Fragment2"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 fragment:- <?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"
- android:background="#cbffba">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="Fragment3"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 activity_main:- <?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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:id="@+id/frame1"
- android:visibility="visible"
- android:orientation="horizontal"></FrameLayout>
- </LinearLayout>
複製代碼- <?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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:baselineAligned="false">
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="2"
- android:id="@+id/frame1"></FrameLayout>
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="3"
- android:id="@+id/frame2"></FrameLayout>
- </LinearLayout>
複製代碼 activity_main(land):- <?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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:baselineAligned="false">
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="1.79"
- android:id="@+id/frame1"></FrameLayout>
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="3"
- android:id="@+id/frame2"></FrameLayout>
- </LinearLayout>
複製代碼 |