Android-Fragment的创建

Fragment的创建

一.静态Fragment的创建

首先在left_fragment.xml中添加一个人Button按钮,right_fragment.xml中添加一个textView;
其次分别创建对应的.java文件,让LeftFragment和RightFragment都继承Fragment,并重写onCreatView()方法。如:

public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);//将left_fragment布局加载进来
return view;
}
}

最后在activity_main.xml中:

android:id="@+id/left_fragment"
android:name=“com.example.fragmenttest.LeftFragment”//通过这个属性来显示的指明要添加的碎片类名,注意一定要将类的包名也加上
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”/>

二.动态Fragment的创建

首先在left_fragment.xml中添加一个人Button按钮,right_fragment.xml中添加一个textView;
其次分别创建对应的.java文件,让LeftFragment和RightFragment都继承Fragment,并重写onCreatView()方法。如:

public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment, null);
}
}

然后在activity_main.xml中添加两个容器:



最后在MainActivity.java中:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    //FragmentManager:Activity内部用来与Fragment进行交互的接口
    FragmentManager fragmentmanager = getFragmentManager();
    //开启一个事务
    FragmentTransaction transaction = fragmentmanager.beginTransaction();
    
    LeftFragment leftfragment = new LeftFragment();
    RightFragment rightfragment = new RightFragment();
    //将左侧的那个leftfragment添加到R.id.left_fragment中
    transaction.add(R.id.left_fragment, leftfragment);
    transaction.add(R.id.right_fragment, rightfragment);
    
    transaction.commit();//提交事务
}

}

静态加载的方式

1.自定义一个类FragmentLeft继承系统的Fragment
public class FragmentLeft extends Fragment{
public View onCreateView(LayoutInflater inflater ,ViewGroup container , Bundle savedInstanceState){
View view = inflater.inflate(R.laryout.activity_left);
return view;
}
}

FragmentLeft的布局

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#5f00"
    android:orientation="vertical" >



2.再自定义一个FragmentRight继承系统Fragment
public class FragmentRight extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* 1.要加载的布局的资源id
* 2.当前布局的父布局
* 3.是否需要追加到父布局
*/
View view = inflater.inflate(R.layout.activity_fragmentright, container, false);
return view;
}
}

FragmentRight的布局

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#50f0"
    android:orientation="vertical" >



3.MainActivity
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

ainActivity布局
在main_activity中注册fragment标签




动态加载Fragment
1.自定义类FragmentLeft继承Fragment
public class FragmentLeft extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_left, container,false);
return view;
}
}

FragmentLeft布局

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >



2.自定义FragmentRight继承Fragment
public class FragmentRight extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_right, container,false);
return view;
}
}

FragmentRight布局

android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >



3.MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取Fragment的管理者对象 FragmentManager:用来在Framgent和Activity之间交互的接口
FragmentManager fragmentManager = getFragmentManager();
//将add remove replace等等操作称为事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
//参数1表示fragment添加到Activity的区域的id 2.表示需要具体添加的Fragment对象
transaction.add(R.id.layout_container_leftFragment, new FragmentLeft());
transaction.add(R.id.layout_container_rightFragment, new FragmentRight());

    /**
     *transaction.remove(fragment);
     *transaction.replace(containerViewId, fragment);
     *transaction.hide(fragment)
     *transaction.show(fragment);
     */
    //提交事务--->保存
    transaction.commit();
}

在activity_main布局里面添加两个LinearLayout用于放Fragment


     

你可能感兴趣的:(Android)