สร้าง Fragment ขึ้นมาใหม่ชื่อ FirstFragment, SecondFragment และ ThirdFragment
ไฟล์ที่เกี่ยวข้อง
- activity_main.xml
- MainActivity.java
- fragment_first.xml
- FirstFragment.java
- fragment_second.xml
- SecondFragment.java
- fragment_third.xml
- ThirdFragment.java
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" android:orientation="vertical" tools:context=".MainActivity"> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="20dp"> <Button android:id="@+id/button1" android:text="Fragment 1" android:textSize="16sp" /> <Button android:id="@+id/button2" android:text="Fragment 2" android:textSize="16sp" /> <Button android:id="@+id/button3" android:text="Fragment 3" android:textSize="16sp" /> </GridLayout> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
package com.phaisarn.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final FirstFragment f1 = FirstFragment.newInstance("First value!"); final FirstFragment f2 = FirstFragment.newInstance("Second value!"); final FirstFragment f3 = FirstFragment.newInstance("Third value!"); Button button1 = findViewById(R.id.button1); Button button2 = findViewById(R.id.button2); Button button3 = findViewById(R.id.button3); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f1); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f2); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f3); } }); } private void replaceFragment(Fragment fragment) { getSupportFragmentManager() .beginTransaction() .replace(R.id.frameLayout, fragment) .commit(); } }
บรรทัดที่ 17-19 สร้าง object ของ fragment
ขึ้นมาใหม่จาก static method ของ fragment
พร้อมส่งค่าที่ต้องการให้ fragment
fragment_first.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FirstFragment"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="30dp" android:text="Fragment #1" android:textSize="20sp" /> </FrameLayout>
FirstFragment.java
package com.phaisarn.myapplication; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FirstFragment extends Fragment { private static final String MESSAGE1 = "message1"; public FirstFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, container, false); TextView textView = view.findViewById(R.id.textView1); String msg1 = getArguments().getString(MESSAGE1); textView.setText(msg1); return view; } public static FirstFragment newInstance(String msg1) { FirstFragment fragment = new FirstFragment(); Bundle bundle = new Bundle(); bundle.putString(MESSAGE1, msg1); fragment.setArguments(bundle); return fragment; } }
บรรทัดที่ 30 สร้าง static method ชื่อ newInstance()
ไว้รับค่าให้กับ fragment
บรรทัดที่ 32 การส่งค่าให้ fragment
ต้องทำผ่าน Bundle
บรรทัดที่ 34 ส่งค่าให้ fragment
บรรทัดที่ 24 การรับค่าที่ส่งมาให้ fragment
รับค่าด้วย getArguments
บรรทัดที่ 25 แสดงข้อความออก textview
fragment_second.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondFragment"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="30dp" android:text="Fragment #2" android:textSize="20sp" /> </FrameLayout>
SecondFragment.java
package com.phaisarn.myapplication; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class SecondFragment extends Fragment { private static final String MESSAGE2 = "message2"; public SecondFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, container, false); TextView textView = view.findViewById(R.id.textView1); String msg1 = getArguments().getString(MESSAGE2); textView.setText(msg1); return view; } public static FirstFragment newInstance(String msg1) { FirstFragment fragment = new FirstFragment(); Bundle bundle = new Bundle(); bundle.putString(MESSAGE2, msg1); fragment.setArguments(bundle); return fragment; } }
fragment_third.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ThirdFragment"> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="30dp" android:text="Fragment #3" android:textSize="20sp" /> </FrameLayout>
ThirdFragment.java
package com.phaisarn.myapplication; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ThirdFragment extends Fragment { private static final String MESSAGE3 = "message3"; public ThirdFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, container, false); TextView textView = view.findViewById(R.id.textView1); String msg1 = getArguments().getString(MESSAGE3); textView.setText(msg1); return view; } public static FirstFragment newInstance(String msg1) { FirstFragment fragment = new FirstFragment(); Bundle bundle = new Bundle(); bundle.putString(MESSAGE3, msg1); fragment.setArguments(bundle); return fragment; } }