原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 {  
    //创建视图  
    @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("hi");  
        return view;  
    }  
    @Override  
    public void onAttach(Context context) {  
        super.onAttach(context);  
    }  

    @Override  
    public void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
    }  

    @Override  
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {  
        super.onActivityCreated(savedInstanceState);  
    }  

    @Override  
    public void onStart() {  
        super.onStart();  
    }  

    @Override  
    public void onPause() {  
        super.onPause();  
    }  

    @Override  
    public void onStop() {  
        super.onStop();  
    }  

    @Override  
    public void onDestroyView() {  
        super.onDestroyView();  
    }  

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
    }  
}  

在容器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">  

   <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.content.Context;  
import android.content.Intent;  
import android.support.annotation.Nullable;  
import android.support.v4.app.Fragment;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.Button;  

public class MainActivity extends AppCompatActivity {  

    @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关联  
        ListFragment fragment = new ListFragment();  
        getSupportFragmentManager()  
                .beginTransaction()  
                .add(R.id.listContainer,fragment)  
                .commit();  
        getSupportFragmentManager()  
                .beginTransaction()  
                .add(R.id.detailContainer,new ListFragment())  
                .commit();  
        //new ListFragment()新建一个fragment  

//        //移除fragment  
//        getSupportFragmentManager()  
//                .beginTransaction()  
//                .remove(fragment)  
//                .commit();  

//        //替换fragment  
//        getSupportFragmentManager()  
//                .beginTransaction()  
//                .replace(R.id.listContainer,new ListFragment())  
//                .commit();  
    }  

}