สร้าง Fragment ขึ้นมาใหม่ชื่อ DynamicFragment
เรียกใช้ DynamicFragment จาก source code โดยสามารถ show ได้และ hide ได้
ไฟล์ที่เกี่ยวข้อง
- activity_main.xml
- MainActivity.java
- fragment_dynamic.xml
- DynamicFragment.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/buttonShow" android:text="Show Fragment" android:textAllCaps="false" android:textSize="18sp" /> <Button android:id="@+id/buttonHide" android:text="Hide Fragment" android:textAllCaps="false" android:textSize="18sp" /> </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 androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; 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 DynamicFragment fragment = new DynamicFragment(); Button buttonShow = findViewById(R.id.buttonShow); buttonShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFragment(fragment); } }); Button buttonHide = findViewById(R.id.buttonHide); buttonHide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hideFragment(fragment); } }); } private void showFragment(Fragment fragment) { FragmentManager fragMan = getSupportFragmentManager(); FragmentTransaction fragTran = fragMan.beginTransaction(); if (!fragment.isAdded()) { fragTran.add(R.id.frameLayout, fragment); } else if (fragment.isHidden()) { fragTran.show(fragment); } fragTran.commit(); } private void hideFragment(Fragment fragment) { if (fragment.isAdded() && fragment.isVisible()) { getSupportFragmentManager() .beginTransaction() .hide(fragment) .commit(); } } }
fragment_dynamic.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" android:background="#cde" tools:context=".DynamicFragment"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="ข้อความนี้อยู่ใน Fragment" android:textSize="20sp" /> </FrameLayout>
DynamicFragment.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; public class DynamicFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) { return inflater.inflate(R.layout.fragment_dynamic, viewGroup, false); } }