Fragment其实就是一个小的Activity,他的生命周期和Activity差不多。Fragment概念的提出就是因为平板编程的需要,Fragment使得UI设计得以模块化,平板等大屏设备的UI设计得到了很好的技术支持。然而Fragment概念的提出是因为大屏UI设计的需求,但是使用并不局限于平板设计。下面这坨代码(的确是坨。。)实现的是一个类似微信的选项卡切换界面的功能,全局只有一个Activity,真正改变的只是Fragment。
Activity:
package com.example.myfragment;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private FragmentManager fm;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv4 = (TextView) findViewById(R.id.text4);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this);
fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
fragment4 = new Fragment4();
ft.replace(R.id.content, fragment1);
ft.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.text1:
ft.replace(R.id.content, fragment1);
break;
case R.id.text2:
ft.replace(R.id.content, fragment2);
break;
case R.id.text3:
ft.replace(R.id.content, fragment3);
break;
case R.id.text4:
ft.replace(R.id.content, fragment4);
break;
}
ft.commit();
}
}
activity 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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text1" />
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text2" />
<TextView
android:id="@+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text3" />
<TextView
android:id="@+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text4" />
</LinearLayout>
<RelativeLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
fragment1:
package com.example.myfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
f1 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="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment1"
android:textSize="20dp" >
</TextView>
</LinearLayout>
fragment2到fragment4的定义方式和fragment1类似,这里就不贴出来了。