fragment基础

1.创建静态fragment
    写一个类继承fragment
     重写onCreateView();return view
         private TextView tv_firstFrag;

        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View view = inflater.inflate(R.layout.layout_firstfragment, null);
                tv_firstFrag  = (TextView) view.findViewById(R.id.tv_firstFrag);
                tv_firstFrag.setText("在fragment给控件赋值");

                return view;
        }
<fragment
android:id="@+id/firstFragment"
android:name="comvvitamio.cc.com.myapplicationdosometest.FirstFragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
这里name指向重写了fragment的方法
如果要在Java代码中动态的 生成你的内容的话可以这样:
tv_first= (TextView) findViewById(R.id.tv_firstFrag);
tv_first.setText("activity_______>fragment");

2.动态生成一个fragment

    1.用frameLayout占位置,之后会被替换
<!--     声明碎片的位置,待会会被替换 -->

<FrameLayout
android:id="@+id/replaceId"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/firstFragment" />
2.写一个类来继承fragment与静态那边基本一样
3.在activity中动态试使用
     一个事物只能被提交一次
fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
SecondFragment fragment=new SecondFragment();
transaction.replace(R.id.replaceId,fragment);
transaction.commit();
与静态一样,如果没有 动态生成内容的话,就是XML里面的内容,动态添加也可以

一个完整的动态例子

//一个事务只能被提交一次
FragmentTransaction beginTransaction = manager.beginTransaction();
BottomFragment fragment = new BottomFragment();
Bundle bundle = new Bundle();
bundle.putString("msg", content+new Date());
//传递内容到fragment里面
fragment.setArguments(bundle);
beginTransaction.replace(R.id.replaceId, fragment);

    利用bundle传递数据的例子
Bundle bundle = new Bundle();
bundle.putString("msg", content+new Date());
//传递内容到fragment里面
fragment.setArguments(bundle);
Bundle bundle = getArguments();
if (bundle != null) {
String string = bundle.getString("msg");
tv_bottom.setText(string);
}
fragment的生命周期    
---------------->自己去看吧,现在不想搞。看了记得补充笔记


对于一个动态fragment之间互相传递数据的例子
step1:
private FragmentManager manager;
step2:
manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.leftReplaceId,new LeftFragment());
transaction.add(R.id.rightRepaceId,new RightFragment());
transaction.commit();
主函数的方法
public void doFileStr(String s){
FragmentTransaction transaction=manager.beginTransaction();
RightFragment fragment=new RightFragment();
Bundle bundle=new Bundle();
bundle.putString("msg",s);
fragment.setArguments(bundle);
transaction.replace(R.id.rightRepaceId,fragment);
transaction.commit();
点击发送左边fragment的数据
fileStr="xxxxxxxxxxxxxxx";
LeftAndRightFragment fragment= (LeftAndRightFragment) getActivity();
fragment.doFileStr(fileStr);
右边对数据的处理与显示
textView = (TextView) view.findViewById(R.id.textView);

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值