建立一个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();