原CSDN博客已弃用,文章会逐渐迁移过来。

xml布局文件

fragment_list.xml (控制fragment里面的内容)

    <?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent" android:layout_height="match_parent"  
android:background="#ffff00">  
<TextView  
    android:id="@+id/textView"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_centerInParent="true"  
    android:text="TextView"  
    android:textColor="#000000"  
    android:textSize="20sp"/>  
</RelativeLayout>  

Fragment的Java代码管理类

    package com.administrator.fragmentdemo;  

import android.content.Context;  
import android.os.Bundle;  
import android.support.annotation.Nullable;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.TextView;  

/** 
 * 列表fragment 
 */  
public class ListFragment extends Fragment {  

    public static final String BUNDLE_TITLE = "bundle_title";  
    private String mTitle = "mo_ren_zhi";  

    public static ListFragment newInstance(String title){  
        ListFragment fragment = new ListFragment();  
        Bundle bundle = new Bundle();//创建一个bundle对象对数据封装  
        bundle.putString(BUNDLE_TITLE,title);//put一下,有很多比如char,int等基础数据,比如这里用String(key,Value),还可以比如putChar();  
        fragment.setArguments(bundle);//将封装好的bundle数据,通过fragment的setArgument方法传递。  
        return fragment;  
    }  
    //创建视图  
    @Nullable  
    @Override  
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        //new view  
        View view = inflater.inflate(R.layout.fragment_list,container,false);  
        TextView textView = view.findViewById(R.id.textView);//需要根据根视图view进行findViewById  
        textView.setText(mTitle);  
        textView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if(mOnTitleClickListener!=null){  
                    mOnTitleClickListener.onClick(mTitle);  
                }  
            }  
        });  
        return view;  
    }  
    @Override  
    public void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        if(getArguments() != null){  
            mTitle = getArguments().getString(BUNDLE_TITLE);  
        }  
    }  

    //设置接口的方法  
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener){  
        mOnTitleClickListener = onTitleClickListener;  
    }  

    //定义变量  
    private OnTitleClickListener mOnTitleClickListener;  
    /** 
     * 当title被点击时,将title传递出去 
     */  
    //定义接口  
    public interface OnTitleClickListener{  
        void onClick(String title);  
    }  
}  

在容器activity_main.xml中为fragment设置可以展示的区域

分别设置了listContainer和detailContainer这两个容器

    <?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="com.administrator.fragmentdemo.MainActivity">  

   <TextView  
       android:id="@+id/textView"  
       android:layout_width="match_parent"  
       android:layout_height="100dp"  
       android:gravity="center"  
       android:text="static load fragment"/>  
   <LinearLayout  
       android:orientation="horizontal"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent">  
      <LinearLayout  
          android:id="@+id/listContainer"  
          android:layout_width="150dp"  
          android:layout_margin="1dp"  
          android:layout_height="match_parent">  
      </LinearLayout>  
      <LinearLayout  
          android:id="@+id/detailContainer"  
          android:layout_width="200dp"  
          android:layout_margin="1dp"  
          android:layout_height="match_parent">  
      </LinearLayout>  
   </LinearLayout>  

</LinearLayout>  

装载Fragment的Activity的Java代码

    package com.administrator.fragmentdemo;  

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  

public class MainActivity extends AppCompatActivity implements ListFragment.OnTitleClickListener{  

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


        //1.container容器 2.fragment 3.fragment-->container把fragment放到容器container中去  
        //getSupportFragmentManager()获取一个fragment容器,beginTransaction()开启一个事物,add()把fragment放进去(关联进去)。commit()向系统提交  
        //R.id.listContainer,listContainer是刚才设置的容器id,add里面把容器和fragment关联  

        //fragment---> activity value  fragment往activity里传值  
        ListFragment listFragment = ListFragment.newInstance("list");  
        getSupportFragmentManager()  
                .beginTransaction()  
                .add(R.id.listContainer,listFragment)  
                .commit();  
        listFragment.setOnTitleClickListener(this);  

        ListFragment detail = new ListFragment().newInstance("detail");  
        getSupportFragmentManager()  
                .beginTransaction()  
                .add(R.id.detailContainer,detail)  
                .commit();  
        detail.setOnTitleClickListener(this);  

    }  

    @Override  
    public void onClick(String title) {  
        setTitle(title);  
    }  
}  

比如说要传一个对象,不能用bundle传的时候

更改ListFragment.java文件

    package com.administrator.fragmentdemo;  

import android.content.Context;  
import android.os.Bundle;  
import android.support.annotation.Nullable;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.TextView;  

/** 
 * 列表fragment 
 */  
public class ListFragment extends Fragment {  

    public static final String BUNDLE_TITLE = "bundle_title";  
    private String mTitle = "mo_ren_zhi";  

    /** 
     * 构建模拟的User对象 
     */  
    private User mUser;  
    public void setUser(User user){  
        mUser = user;  
    }  
    public class User{  

    }  

    public static ListFragment newInstance(String title,User user){//添加User参数  
        ListFragment fragment = new ListFragment();  
        Bundle bundle = new Bundle();//创建一个bundle对象对数据封装  
        bundle.putString(BUNDLE_TITLE,title);//put一下,有很多比如char,int等基础数据,比如这里用String(key,Value),还可以比如putChar();  
        fragment.setArguments(bundle);//将封装好的bundle数据,通过fragment的setArgument方法传递。  
        fragment.setUser(user);//设置User对象  
        return fragment;  
    }  
    //创建视图  
    @Nullable  
    @Override  
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        //new view  
        View view = inflater.inflate(R.layout.fragment_list,container,false);  
        TextView textView = view.findViewById(R.id.textView);//需要根据根视图view进行findViewById  
        textView.setText(mTitle);  
        textView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if(mOnTitleClickListener!=null){  
                    mOnTitleClickListener.onClick(mTitle);  
                }  
            }  
        });  
        return view;  
    }  
    @Override  
    public void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        if(getArguments() != null){  
            mTitle = getArguments().getString(BUNDLE_TITLE);  
        }  
    }  

    //设置接口的方法  
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener){  
        mOnTitleClickListener = onTitleClickListener;  
    }  

    //定义变量  
    private OnTitleClickListener mOnTitleClickListener;  
    /** 
     * 当title被点击时,将title传递出去 
     */  
    //定义接口  
    public interface OnTitleClickListener{  
        void onClick(String title);  
    }  
}