静态添加碎片:
left_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d0d00a"
android:text="left_Fragment" />
</LinearLayout>
right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d0d0d0"
android:text="right_Fragment" />
</LinearLayout>
LeftFragment.java
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment, null);
}
}
RightFragment.java
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.right_fragment, null);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/left_layout"
android:name="com.example.fragment01.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_layout"
android:name="com.example.fragment01.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
动态添加碎片:
动态添加Fragment与静态的差别还是很大的:
动态添加的时候需要用到FragmentManager类下的getFragmentManager来进行与Activity进行交互。它相当于一个接口用于和Activity进行交互。
**left_fragment.xml 和right_fragment.xml中的代码完全一样
以及LeftFragment.java 和RightFragment.java中的代码也是完全一样**
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/fl_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/fl_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
MainActivity.java
package com.example.fragment01;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
private FragmentManager fManager;// 是Fragment和Activity进行交互的接口
private FragmentTransaction transaction;// 开启一个事务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
transaction = fManager.beginTransaction();
// 添加左侧的布局
LeftFragment leftFragment = new LeftFragment();
transaction.add(R.id.fl_left, leftFragment);
// 添加右侧的布局
RightFragment rightFragment = new RightFragment();
transaction.add(R.id.fl_right, rightFragment);
// 提交事务
transaction.commit();
}
}
Fragment的生命周期
1.onAttach():与Activity进行关联。
2.onCreate():创建一个Fragment的时候调用。
3.onCreateView():创建一个Fragment视图的时候调用。
4.onActivityCreated():确定与碎片相关联的活动一定已经被创建的时候调用。
5.onStart():Fragment开始的时候调用。
6.onResume():这个方法在Fragment准备与用户进行交互的时候调用。
6.onPause():当Fragment准备去启动或者恢复另一个Fragment的时候调用。
7.onStop():当它的宿主Activity停止的时候或者该Fragment停止的时候调用此方法。
8.onDestroyView():销毁一个Fragment视图的时候调用。
9.onDestroy():销毁一个Fragment的时候或者它的宿主Activity销毁的时候盗用此方法。
10.onDetach():当Fragment与Activity不再有关联的时候调用。
Fragment的传值。
利用setArguments(Bundle b)和getArguments();其中的bundle是用来携带数据的。
在这里我又新建了一个项目:
content_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受值"
android:textSize="20sp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
ContentFragment.java
package com.example.fragment02;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class ContentFragment extends Fragment {
private Button get;
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment, null);
get = (Button) view.findViewById(R.id.get);
tv = (TextView) view.findViewById(R.id.tv);
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = getArguments();
if (b != null) {
String value = b.getString("kk", "");
tv.setText(value);
}
}
});
return view;
}
}
然后是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.fragment02.MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="传值"
android:textSize="20sp" />
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#dddddd"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
最后在MainActivity.java中的代码
package com.example.fragment02;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = manager.beginTransaction();
ContentFragment fragment = new ContentFragment();
transaction.add(R.id.framelayout, fragment);
// 利用bundle携带值
Bundle bundle = new Bundle();
bundle.putString("kk", "你好,Fragment传值");
fragment.setArguments(bundle);
transaction.commit();
}
});
}
}