fragment 与activity通信 Bundle 序列化数据 Serializable

1、fragment简单套用(静态调用):

新建一个fragment,其xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#00ff00" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="This is fragment 1"  
        android:textColor="#000000"  
        android:textSize="25sp" />  
  
</LinearLayout>  

其java文件如下:

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

在主Activity的布局中加入:

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

这样就可以了;

2、动态使用fragment

首先,Activity的布局中不用加fragment:

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

在java中:

Fragment1 fragment1 = new Fragment1();  
            getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit(); 

分四步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。

若是想在Fragment中调用Activity中的函数,可直接使用(MainActivity)getActivity()来获取活动;

若想在创建Fragment时,从Activity中传递数据给Fragment,可利用Bundle:

        FriendsFragment friendsFragment = new FriendsFragment();
        Bundle bundle = new Bundle();
        serializable = new DataSerializable();
        serializable.setListFriend(myFriendList);
        bundle.putSerializable("list_friend", serializable);
        friendsFragment.setArguments(bundle);
        fragmentList.add(friendsFragment);

Fragment代码中:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_friends, container, false);

        DataSerializable dataSerializable = (DataSerializable)getArguments().getSerializable("list_friend");
        List<MyFriend> myFriendsList = dataSerializable.getListFriend();

        return view;
    }

其中,DataSerializable是自己实现的一个类,实现了Serializable接口,这样就可以在Bundle中存储更多复杂类型的数据,简单数据比如int,直接用Bundle.putint();

DataSerializable代码如下:

public class DataSerializable implements Serializable {
    private List<MyFriend> myFriendList;

    public void setListFriend(List<MyFriend> myFriendList){
        this.myFriendList = myFriendList;
    }
    public List<MyFriend> getListFriend(){
        return myFriendList;
    }
}

如果在Activity中想调用Fragment中的函数:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
原文地址:https://www.cnblogs.com/zhaozilongcjiajia/p/10425180.html