標題:
Fragment (二)
[打印本頁]
作者:
tonyh
時間:
2019-8-9 15:27
標題:
Fragment (二)
本帖最後由 tonyh 於 2019-8-9 15:28 編輯
layout\activity_main.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: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>
複製代碼
layout-land\activity_main.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: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>
複製代碼
layout\fragment1.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"
android:background="#c0e6ff">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
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="This is 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>
複製代碼
layout\fragment2.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"
android:background="#fed4f4">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
layout\fragment3.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"
android:background="#daffce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
MainActivity.java
package org.istak.ch38_fragment;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 f1 = new Fragment1();
//FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frame1, f1, null);
ft.commit();
}
}
複製代碼
Fragment1.java
package org.istak.ch38_fragment;
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;
/**
* Created by Tony on 2017/11/14.
*/
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 v=inflater.inflate(R.layout.fragment1,container,false);
btn1= (Button) v.findViewById(R.id.button1);
btn2= (Button) v.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) //若 frame2 存在,則代表此時裝置是橫向擺放的 (橫擺時會自動抓資料夾 layout-land 中的布局)
{
ft.add(R.id.frame2, f2, null); //將 f2 加到 frame2 上 (畫面的右側)
}else{
ft.add(R.id.frame1, f2); //將 f2 加到 frame1 上 (畫面的左側)
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 v;
}
}
複製代碼
Fragment2.java
package org.istak.ch38_fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Tony on 2017/11/14.
*/
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment2,container,false);
return v;
}
}
複製代碼
Fragment3.java
package org.istak.ch38_fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Tony on 2017/11/14.
*/
public class Fragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment3,container,false);
return v;
}
}
複製代碼
作者:
張健勳
時間:
2019-8-31 16:52
<?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>
複製代碼
<?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="#c0e6ff">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
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="This is 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>
複製代碼
<?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="#fed4f4">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
<?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="#daffce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
package com.example.student.d20190831;
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;
import com.example.student.d20190831.Fragment2;
import com.example.student.d20190831.Fragment3;
import com.example.student.d20190831.MainActivity;
import com.example.student.d20190831.R;
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 v=inflater.inflate(R.layout.fragment1,container,false);
btn1= (Button) v.findViewById(R.id.button1);
btn2= (Button) v.findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 f2 = new Fragment2();
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 v;
}
}
複製代碼
package com.example.student.d20190831;
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 v=inflater.inflate(R.layout.fragment2,container,false);
return v;
}
}
複製代碼
package com.example.student.d20190831;
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 v=inflater.inflate(R.layout.fragment3,container,false);
return v;
}
}
複製代碼
package com.example.student.d20190831;
import android.support.v7.app.AppCompatActivity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frame1,f1,null);
ft.commit();
}
}
複製代碼
作者:
李知易
時間:
2019-8-31 17:12
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>
複製代碼
作者:
黃璽安
時間:
2019-9-1 13:49
本帖最後由 黃璽安 於 2019-9-1 13:51 編輯
<?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>
複製代碼
<?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="#c0e6ff">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
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="This is 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>
複製代碼
<?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="#fed4f4">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
<?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="#daffce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is 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>
複製代碼
package com.example.shain.fragment2;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 f1 = new Fragment1();
//FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.frame1, f1, null);
ft.commit();
}
}
複製代碼
package com.example.shain.fragment2;
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 v = inflater.inflate(R.layout.fragment1, container, false);
btn1 = (Button) v.findViewById(R.id.button1);
btn2 = (Button) v.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 v;
}
}
複製代碼
package com.example.shain.fragment2;
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 v = inflater.inflate(R.layout.fragment2, container, false);
return v;
}
}
複製代碼
package com.example.shain.fragment2;
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 v = inflater.inflate(R.layout.fragment3, container, false);
return v;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2