dynamic_fragment.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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="#cde"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="ข้อความนี้อยู่ใน Fragment" android:textSize="20sp"/> </LinearLayout> |
DynamicFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.support.v4.app.Fragment; public class DynamicFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) { return inflater.inflate(R.layout.dynamic_fragment, viewGroup, false); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.phaisarn.myapplication; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.support.v4.app.Fragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final DynamicFragment f1 = new DynamicFragment(); Button buttonShow = findViewById(R.id.buttonShow); buttonShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFragment(f1); } }); Button buttonHide = findViewById(R.id.buttonHide); buttonHide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hideFragment(f1); } }); } 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(); } } } |