Fragment

定义:

     ......

创建Fragment:

public class NewFragment extends Fragment{
    
    public View onCreateView (LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        // 解析XML文件并且实例化
        View v = inflate.infalte(R.id.new,null);
        return v;
    }
}

参数传递:

 1.intent  

     用intent传递参数是可以达到参数传递的作用的,但是这样Fragment就和某个Activity绑定了,不能达到复用的效果。

 2. Fragment Arguments

     传参数:

        Bundle bundle = new Bundle();
        bundle.putBoolean("is_launch", false);
        SportFragment sportFragment = new SportFragment();
        sportFragment.setArguments(bundle);
        getSupportFragmentManager().
                beginTransaction().
                add(R.id.frag,sportFragment).
                commit();

     获得参数:

        boolean isLaunch = getArguments().getBoolean("is_launch",false);

在Activity添加Fragment:

   1.在文件中添加Fragment:
       

     

        adnroid:name = "...."

         ......./>

    2.在Activity运行中添加Fragment:

      

DetailFragment details = new DetailFragment();
// 实例化自定义的碎片类
FragmentTransaction ft = getFragmentManger().beginTransaction();
// 获得一个FragmentTransaction 的实例
ft.add(android.R.id.content,details);
// 添加碎片到Activity中
// android.R.id.content是一个ViewGroup
ft.commit();
//提交事务

 

你可能感兴趣的:(Android)