Fragment Review
参考资料
Fragment一般重写3个方法:
1. onCreateView() 用来绘制UI <必须重写>
2. onCreate() 常用于加载数据
3. onDestroy() 常用于注销广播
Fragment与Activity的通讯
1.通过getActivity()访问Activity的实例
2.通过findFragmentById()或者findFragmentByTag()获取fragment的实例
Sample
1.添加Tag
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.replace(R.id.ll1, new Fragment1(),"f1");
beginTransaction.replace(R.id.ll2, new Fragment2(),"f2");
beginTransaction.commit();
2.调用f2中的updatetext()
view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 f2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2");
f2.updateTextView("Updated Content");
}
});
网友评论