Fragment学习总结

一、Fragment的生命周期的特点及注意点:
1. onAttach:onAttach()在fragment与Activity关联之后调调查用。需要注意的是,初始化fragment参数可以从getArguments()获得,但是,当Fragment附加到Activity之后,就无法再调用setArguments()。所以除了在最开始时,其它时间都无法向初始化参数添加内容
2. onCreate:fragment初次创建时调用。尽管它看起来像是Activity的OnCreate()函数,但这个只是用来创建Fragment的。此时的Activity还没有创建完成,因为我们的Fragment也是Activity创建的一部分。所以如果你想在这里使用Activity中的一些资源,将会获取不到。比如:获取同一个Activity中其它Frament的控件实例。(代码如下:),如果想要获得Activity相关联的资源,必须在onActivityCreated中获取。
3. onActivityCreated:在Activity的OnCreate()结束后,会调用此方法。所以到这里的时候,Activity已经创建完成!在这个函数中才可以使用Activity的所有资源。如果把下面的代码放在这里,获取到的btn_Try的值将不会再是空的!
其他的生命周期与activity相似。
二、使用Fragment的方式:有两种静态和动态。
1. 静态的方式是:在layout中布局fragment的,如下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:baselineAligned="false" >  

    <fragment  
        android:id="@+id/fragment1"  
        android:name="com.harvic.com.harvicblog2.Fragment1"  
        android:layout_width="0dip"  
        android:layout_height="match_parent"  
        android:layout_weight="1" />  

    <fragment  
        android:id="@+id/fragment2"  
        android:name="com.harvic.com.harvicblog2.Fragment2"  
        android:layout_width="0dip"  
        android:layout_height="match_parent"  
        android:layout_weight="1" />  

</LinearLayout> 
        2. 动态的方式是:使用FragmentTransaction的add方法或者replace方法,动态改变布局。

三、使用FragmentManager和FragmentTransaction 对Fragment的管理,显示隐藏。对Fragment的显示和隐藏有add、replace、hide、show、detach()、attach()。
1. add:主要是通过覆盖的方式来达到替换,而且不能重复添加相同ID的Fragment。
2. replace是清除最上层的fragment,并用的新的fragment作为最上层的fragment。replace与add一起使用,在使用返回键时会出现错误。
3. hide、show、detach、attach,popBackStack他们是以一次commute我单位的,在每次add中都要使用addToBackStack方法,每次使用attach都会作为顶层出现。hide、show不会创建实力,replace每次回创建实例。
4. 回退札popBackStack,前提是札里面有实例;
四、Fragment之间的数据传递。思路有三种,一、在一个activity中写,使用全局变量来实现数据的传递。二、在各种的Fragment的做数据处理,再传递,跟onclick相似,这里需要借助activity来获取相应的Fragment,从而在实现在另一个fragment操作数据。三、使用的接口回调的方式,来实现的。
五、fragment的保存有三种:一、使用EditText。二使用控件ID,为每个控件增加ID,系统自动保存。三使用视图保存,也是系统会自动保存的。
一的代码如下:`public class Fragment1 extends Fragment {
private String mEditStr;
private EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
editText = (EditText)rootView.findViewById(R.id.fragment1_edittext);
editText.setText(mEditStr);
return rootView;
}

@Override  
public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState);  
    Button btnReplace = (Button)getView().findViewById(R.id.fragment1_repalce);  
    btnReplace.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View v) {  
            mEditStr = editText.getText().toString();  

            …………  
        }  
    });  
    …………  
}  

} `

三的代码如下:

public View onCreateView(LayoutInflater inflater, ViewGroup container,  
        Bundle savedInstanceState) {  
    return getPersistentView(inflater, container, savedInstanceState, R.layout.fragment1);  
}  `
public View getPersistentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, int layout) {  
    if (rootView == null) {  
        // Inflate the layout for this fragment  
        rootView = inflater.inflate(layout, container,false);  
    } else {  
        ((ViewGroup) rootView.getParent()).removeView(rootView);  
    }  
    return rootView;  
} 

`## 回调的思想需要学会。 ##

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值