You are on page 1of 7

1. Fragment l g ? Fragment c th xem nh l 1 hoc nhu giao din (sub activity), c vng i ca n, v thng nm trong 1 mn hnh cng lc.

Khi no nn s dng Fragment ? Khi mnh pht trin ng dng c layout khc nhau ch Portrait v Landscape Khi bn mun ng dng ca mnh h tr nhiu loi mn hnh to nh khc nhau (tablet c mn hnh to hn in thoi nhu)

1/nh ngha: Fragment l mt i tng c nhng vo Activity, chng ta c th xem Fragment nh l mt hoc nhiu giao din (sub Activity) c lifecycle ring v thng nm cng mt mn hnh. Fragment c thm vo t API level 11

o C cc loi Fragment sau: ListFragment DialogFragment WebViewFragment PreferenceFragment


Mun dng c Fragment th u tin bn phi khai bo mt i tng dng qun l Fragment l FragmentManger. C php: y c 2 trng hp nu s dng t API level 11 tr ln th nh sau: FragmentManager manger= getFragmentManager(); Cn dng trong cc API thp hn th: FragmentManager manger= getSupportFragmentmanger(); Sau chng ta cn to ra mt i tng qun l vic thm, xa, sa or thay th Fragment l FragmentTransaction. C php: FragmentTransaction transaction= manager.beginTransaction(); Sau y l mt s method thng dng vi Fragment: Thm fragment: add(intcontainerViewId, Fragment fragment, String tag) Xa fragment: remove(Fragment fragment) Thayth fragment: replace(intcontainerViewId, Fragment fragment, String tag) Ktthc transaction: commit() c bit lu method commit() lun lun phi c sau khi bn dng cc method trn, v lun lun t n cui cng.

2. Lm vic vi Fragment Bc 1: Ci t th vin android support (android-support-v4.jar) t Android SDK Manager, add vo th mc libs Bc 2: To 1 project t tn l VN. Trong phn layout ngoi activity_main t sinh ra th ta to thm 3 fragment na, t tn ln lt n l: fragment_01, fragment_02, start_fragment v trong phn src th ngoi MainActivity th ta to thm 3 class na t tn l Fragment01, Fragment02, StartFragment

Ni dung trong activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.vn.MainActivity" tools:ignore="MergeRootFrame" > <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnStartFragment" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="ChonFragment1" android:text="Start Fragment" /> <Button android:id="@+id/btnFragment01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="ChonFragment1" android:text="Fragment 01" /> <Button android:id="@+id/btnFragment02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="ChonFragment1" android:text="Fragment 02" /> </LinearLayout> <LinearLayout android:id="@+id/fragment_placeholder" android:layout_weight="3" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </LinearLayout>

Ni dung trong fragment_01.xml


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="day la Fragment 01" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>

Ni dung trong fragment_02.xml


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.widget.TextView android:id="@+id/fragment_placeholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="day la Fragment 02" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>

Ni dung trong start_fragment.xml


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.widget.TextView android:id="@+id/btnStartFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="day la Start Fragment" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>

Bc 3: Trong phn src ti MainActivity.java - to 1 fragment, bn phi k tha lp Fragment. - Lu khi to Fragement, bn phi override li hm onCreateView().

Ni dung :
package com.example.vn; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // dng trong cc API thp hn FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); StartFragment startFragment = new StartFragment(); transaction.add(R.id.fragment_placeholder, startFragment); transaction.commit(); //do s dng thm sa xa thay th th .commit } public void ChonFragment1(View view) { Fragment newFragment; if(view == findViewById(R.id. btnStartFragment)) { newFragment = new StartFragment(); } else if(view == findViewById(R.id.btnFragment01)) { newFragment = new Fragment01(); } else if(view == findViewById(R.id. btnFragment02)) { newFragment = new Fragment02(); } else { newFragment = new StartFragment(); } //to ra mt i tng qun l vic thm, xa, sa or thay th Fragment l FragmentTransaction

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_placeholder, newFragment); transaction.addToBackStack(null);//mun tt c Fragment u c a vo stack transaction.commit(); } }

Ni dung Fragment01.java
package com.example.vn; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment01 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout. fragment_01,container, false); } }

Ni dung Fragment02.java
package com.example.vn; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment02 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout. fragment_02,container, false); } }

Ni dung StartFragment
package com.example.vn; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class StartFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout. start_fragment,container, false); }

} II.

VIEWPAGER

1) ViewPager l g? - ViewPager l mt i tng kh ging nh Slide trnh din ca MS PowerPoint. - ViewPager c th trt chuyn i gia cc giao din mt cch nh nhng v kh mt, thay v chuyn i mn hnh qua mt s kin chp en nh trn tivi. Mn hnh hin th trc n hoc sau n s c hin th ra ngay tc th lin vi n. - ViewPager h tr t Android API 13 tr ln.

You might also like