Android是在Android 3.0 (API level 11)开始引入Fragment的。
可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。
可以把Fragment设计成可以在多个Activity中复用的模块。
最近刚好一个同学对这块很困惑,小弟不才,写了个demo,自己的理解和用法,欢迎大家批评讨论。
首先创建一个新的Activity 继承自FragmentActivity,有一点注意,使用的fragment都是android.support.v4包下的
MainActivity.class
package com.example.fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//fragment传值
Bundle b = new Bundle();
b.putInt("int", 15);
b.putString("string", "String");
b.putDouble("double", 5.9);
//跳转到fragment
Fragment1 f1 = new Fragment1();
f1.setArguments(b);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frag_content1, f1);
ft.commit();
}
}
上面也简单的介绍了Fragment之间的传值,下面是MainActivity的布局文件: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_content1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
</FrameLayout>
新建两个Fragment,继承自fragment,思路是MainActivity上有Fragment1,再由Fragment1替换Fragment2,而从fragment2也可以返回到fragment1
package com.example.fragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* v4包下的
* @author sunshangfeng
*
*/
public class Fragment1 extends Fragment {
private int ints;
private String strings;
private double doubles;
private Activity activity;
private TextView tvstring;
private TextView tvint;
private TextView tvdouble;
private Button btnclick;
@Override
public void onCreate(Bundle savedInstanceState) {
if(getArguments() != null){
Bundle b = getArguments();
ints = b.getInt("int");
strings = b.getString("string");
doubles = b.getDouble("double");
}
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1, null);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
activity = getActivity();
tvint = (TextView) activity.findViewById(R.id.fragment1_tvint);
tvstring = (TextView) activity.findViewById(R.id.fragment1_tvstring);
tvdouble = (TextView) activity.findViewById(R.id.fragment1_tvdouble);
btnclick = (Button) activity.findViewById(R.id.fragment1_btnclick);
if(ints != 0 && !"".equals(strings)&&doubles != 0.0){
tvint.setText(ints +"");
tvstring.setText(strings);
tvdouble.setText(doubles+"");
}
btnclick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到第二个
Fragment2 f2 = new Fragment2();
//下面这一句是加跳转动画的 动画自己写 你也可以百度 这里是方法
// f2.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
getFragmentManager().beginTransaction() .replace(R.id.frag_content1, f2).commit();
}
});
}
}
fragment2.class
package com.example.fragment;
import android.app.Activity;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment2 extends Fragment {
private Activity activity;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2, null);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
activity = getActivity();
Button btnclick = (Button) activity.findViewById(R.id.fragment2_btnclick);
btnclick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment1 f1 = new Fragment1();
getFragmentManager().beginTransaction() .replace(R.id.frag_content1, f1).commit();
}
});
}
}
两个的布局文件:fragment1.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/fragment1_tvint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/fragment1_tvstring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/fragment1_tvdouble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:id="@+id/fragment1_btnclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳到第二个"/>
</LinearLayout>
fragment2.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/fragment2_btnclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转回第一个ftagment"/>
</LinearLayout>