ITPub博客

首页 > IT职业 > IT职场 > Fragment

Fragment

IT职场 作者:ITpubnet 时间:2021-09-09 09:59:55 0 删除 编辑

1、Fragment的简单使用

        Fragment:在Activity中使用的碎片,有自己的布局、生命周期和输入事件

        使用Fragment的步骤

                (1)创建类,并继承Fragment;

public class FirstFragment extends Fragment{}

                (2)重写Fragment的onCreateView()生命周期方法,并返回一个View;

@Override	public View onCreateView(LayoutInflater inflater, ViewGroup container,			Bundle savedInstanceState) {		// TODO 加载fragment视图(两种方法,推荐使用第二种)		//View view = inflater.inflate(R.layout.first_fragment, null);				View  view2 = inflater.inflate(R.layout.first_fragment, container, false);				TextView textView = (TextView) view2.findViewById(R.id.textView);				//返回加载的视图对象		return view2;

                (3)使用fragment的两种方法

                        a.在布局文件中使用保证碎片显示唯一:id/tag

                        b.动态添加

                                1.获取到一个fragment管理器对象

FragmentManager manager = getFragmentManager();

                                2.通过这个管理器对象要开启一个事务

FragmentTransaction transaction = manager.beginTransaction();

                                3.把预留在布局中的位置,换成要展示的碎片对象

transaction.replace(R.id.replaceId, new SecondFragment());

                                4.提交事务      

transaction.commit();

          使用FragmentManager

                (1)作用:管理多个Fragment之间的交互和传值

                (2)Activity.getFragmentManger()         3.0以后

                (3)FragmentActivity.getSupportFragmentManager() 3.0以前,引用v4包

                (4)FragmentTransaction beginTransaction() 获取Fragment事务处理对象

        s使用FragmentTransaction

                (1)replace(int containerViewId, Fragment fragment)    把预留在布局中的位置,换成要展示的碎片对象

                (2)commit()     提交本次事务处理

3、Fragment的生命周期

        11个生命周期方法

                (1)onAttach(Activity)  连接宿主Activity

                (2)onCreate(Bundle)  创建Fragment

                (3)onCreateView(LayoutInflater, ViewGroup, Bundle)创建Fragment视图

                (4)onActivityCreated(Bundle) 当宿主Activity的onCreate()执行完之后调用

                (5)onStart() 

                (6)onResume() 

                (7)onPause()

                (8)onStop() 

                (9)onDestroyView() 销毁Fragment视图,与onCreateView对应

                (10)onDestroy()  销毁Fragment,与onCreate对应

                (11)onDetach() 与宿主Activity断开连接,与onAttach对应

        生命周期流程

                (1)当Activity创建时,调用Fragment的onAttach->onCreate->onCreateView->onActivityCreated

                (2)当Activity启动时,调用Fragment的onStart

                (3)当Activity获取焦点时,调用Fragment的onResume

                (4)当跳转到另一个Activity时,调用Fragment的onPause-->onStop

                (5)当返回Activity时,调用Fragment的onStart->onResume

                (6)销毁Activity时,调用Fragment的onDestroyView->onDestory->onDettach

3、Fragment与Activity之间的传值

        Activity-->Fragment

                (1)在activity中添加碎片的时候,通过碎片对象的.setArgments(bundle)

public void btnSendMsg(View view){		TextView tv_top = (TextView) findViewById(R.id.tv_top);		tv_top.setText(content+new Date());		FragmentTransaction transaction = manager.beginTransaction();		bottomFragment = new BottomFragment();		Bundle bundle = new Bundle();		bundle.putString("msg", content+new Date());		//把碎片对象和要传递的数据绑定		bottomFragment.setArguments(bundle);		transaction.replace(R.id.replaceId, bottomFragment);//替换的碎片对象中是绑定有数据的		transaction.commit();	}

                (2)在fragment里面,通过getArgments();得到一个bundle对象,再从bundle对象里面获取内容

		Bundle bundle = getArguments();		if (bundle != null) {			String msg = bundle.getString("msg");			tv_bottom.setText(msg);		}

        Fragment-->Activity

                (1)在activity中声明一个公共的方法,在这个方法中必须要有一个参数(参数类型就是要传递的数据类型)

public void setContent(String s){		tv_main.setText(s);	}

                (2)在fragment里面,通过getActivity(),可以获取到宿主activity对象,再调用宿主对象中到提供的公共方法,把数据传递在这个方法中

@Override	public void onClick(View v) {		// TODO 向宿主activity传值				//获取到当前碎片所在的宿主activity对象		MainActivity activity = (MainActivity) getActivity();		activity.setContent(content+new Date());			}

                (3)Activity方式

                        1.在Activity中获取Fragment中的UI控件,并增加相关事件

                        2.在Activity声明公共方法,在Fragment中调用getActivity()并强转,则可以调用公共方法向其他Fragment控件传值

                        3.获取assests下的文件流      InputStream is = getResources().getAssets().open("day01.txt");

原文链接:http://www.apkbus.com/blog-815579-61232.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4556/viewspace-2814849/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    91
  • 访问量
    899