Professional Documents
Culture Documents
Chuong 8
Chuong 8
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
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
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
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) {
} 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.