建立一個Fragmentjava
1.fragment是activity的模塊化,擁有本身的生命週期,能夠在activity運行過程當中添加或移除模塊化
2.建立一個fragment:繼承Fragment類,用XML將fragment添加到Activity。fragment必須與一個FragmentActivity關聯,若是是V7你應該繼承ActionBarActivity他是FragmentActivity的一個子類this
能夠在Activity運行時動態添加移除fragentspa
Fragment之間的交互code
1.在Fragment中定義一個接口,在Activity中實現這個接口繼承
2.在Fragment的onAttach()方法中接受改接口的實現,而後能夠在別處調用接口的方法與activity進行交互接口
ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();