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類似,這里就不貼出來了。
- 前言
- 安卓ListView一個簡單代碼的注釋和探討
- 安卓wifi熱點編程代碼的若干注釋
- 安卓距離傳感器編程
- 簡單的ScrollView
- 簡單的ListView
- 簡單的ArrayAdapter
- AsyncTask的初步學習
- AsyncTask再度學習
- Handler初步學習
- 開啟系統Activity
- 隱式調用Activity
- Activity的生命周期
- startActivityForResult的初步學習
- 多點觸控拉伸圖片
- LruCache圖片緩存技術
- SQLiteOpenHelper數據庫操作
- 短信攔截器
- 簡單的Notification
- ListView優化以及checkbox狀態問題
- 安卓多線程下載
- 安卓JSON解析初步探討
- 選項卡樣式的fragment
- DrawerLayout實現簡單的側滑功能
- 安卓軟引用解決圖片OOM問題
- 安卓隱式Intent啟動Activity和BroadcastReceiver若干注意點
- Dialog學習筆記
- ActionBar使用