生活随笔
收集整理的這篇文章主要介紹了
Android之Fragment使用简介
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Fragment是Android 3.0 (API level 11)后推出的新功能。Android3.0以前的版本也能用Fragment,不過得給工程導(dǎo)入一個(gè)android-support-v4.jar的包。Fragment是一個(gè)有點(diǎn)類似Activity的東西,因?yàn)獒槍Π沧科桨宓南嗬^推出,屏幕越來越大,在一個(gè)這么大的屏幕放一個(gè)Activity顯得布局太大。因此你可以改成放兩個(gè)或多個(gè)Fragment,這些fragment都放在一個(gè)FragmentActivity里。比如安卓官方文檔里的Fragment使用案例就是屏幕左邊放一個(gè)Fragment顯示一個(gè)列表,列表里都是一項(xiàng)一項(xiàng)的文章名稱,然后屏幕右邊放一個(gè)Fragment根據(jù)左邊Fragment選中的文章名稱將文章內(nèi)容顯示在屏幕右邊的Fragment。每個(gè)Fragment里面可以有自己的布局文件,自己做布局,操作起來有點(diǎn)類似Activity。
我這里介紹一個(gè)自己寫的小例子,比較簡單,大家應(yīng)該看得懂。
先來看下主布局文件,主布局文件中底部四個(gè)TextView用來點(diǎn)擊,點(diǎn)擊它們分別會(huì)將屏幕上的Fragment(這個(gè)Fragment就相當(dāng)于上面的LinearLayout)進(jìn)行替換和刪除(具體實(shí)現(xiàn)代碼待會(huì)介紹)
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <RelativeLayout?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:id="@+id/fragmentTop"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="wrap_content"?? ????????/>?? ????<LinearLayout??? ????????android:layout_width="fill_parent"?? ????????android:layout_height="wrap_content"?? ????????android:layout_alignParentBottom="true"?? ????????android:orientation="horizontal"?? ????????>?? ????????<TextView?? ????????????android:id="@+id/text1"?? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"??? ????????????android:layout_weight="1"?? ????????????android:text="FragmentA"?? ????????????android:background="#0000FF"?? ????????????/>?? ????????<TextView?? ????????????android:id="@+id/text2"?? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"??? ????????????android:layout_weight="1"?? ????????????android:text="FragmentB"?? ????????????android:background="#00FF00"?? ????????????/>?? ????????<TextView?? ????????????android:id="@+id/text3"?? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"??? ????????????android:layout_weight="1"?? ????????????android:text="FragmentC"?? ????????????android:background="#FF0000"?? ????????????/>?? ????????<TextView?? ????????????android:id="@+id/text4"?? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"?? ????????????android:layout_weight="1"??? ????????????android:text="刪除Fragment"?? ????????????/>?? </LinearLayout>?? </RelativeLayout>??
然后看看MainActivity,需要注意的地方在代碼里都有解釋下。
[java]?view plaincopy
package?com.jackchan.fragmenttest;?? ?? ?? import?android.os.Bundle;?? import?android.app.Activity;?? import?android.app.FragmentManager;?? import?android.app.FragmentTransaction;?? import?android.app.Fragment;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.widget.TextView;?? import?android.widget.Toast;?? ?? public?class?MainActivity?extends?Activity?{?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ????????TextView?text1?=?(TextView)findViewById(R.id.text1);?? ????????TextView?text2?=?(TextView)findViewById(R.id.text2);?? ????????TextView?text3?=?(TextView)findViewById(R.id.text3);?? ????????TextView?text4?=?(TextView)findViewById(R.id.text4);?? ????????OnItemClickListener?onClickListener?=?new?OnItemClickListener();?? ????????text1.setOnClickListener(onClickListener);?? ????????text2.setOnClickListener(onClickListener);?? ????????text3.setOnClickListener(onClickListener);?? ????????text4.setOnClickListener(onClickListener);?? ????????showDetail(0);?? ?????????? ????}?? ????? ? ? ?? ????private?void?showDetail(int?index){?? ?????????? ?????????? ????????FragmentManager?manager?=?getFragmentManager();?? ????????FragmentTransaction?transaction?=?manager.beginTransaction();?? ????????Fragment?details?=?(Fragment)?? ????????????????getFragmentManager().findFragmentById(R.id.fragmentTop);?? ?????????? ????????switch(index){?? ????????case?0:?? ????????????details?=?new?FragmentInit();?? ????????????transaction.add(R.id.fragmentTop,?details);?? ????????????break;?? ????????case?1:?? ????????????details?=?new?FragmentA();?? ????????????transaction.replace(R.id.fragmentTop,?details);?? ????????????break;?? ????????case?2:?? ????????????details?=?new?FragmentB();?? ????????????transaction.replace(R.id.fragmentTop,?details);?? ????????????break;?? ????????case?3:?? ????????????details?=?new?FragmentC();?? ????????????transaction.replace(R.id.fragmentTop,?details);?? ????????????break;?? ????????case?4:?? ????????????Toast.makeText(this,?details.toString(),?3000).show();?? ????????????transaction.remove(details);?? ????????????break;?? ????????}?? ????????transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);?? ?????????? ????????transaction.addToBackStack(null);?? ????????transaction.commit();?? ????}?? ????class?OnItemClickListener?implements?OnClickListener{?? ?? ????????@Override?? ????????public?void?onClick(View?v)?{?? ?????????????? ????????????if(v.getId()?==?R.id.text1){?? ????????????????showDetail(1);?? ????????????}?? ????????????else?if(v.getId()?==?R.id.text2){?? ????????????????showDetail(2);?? ????????????}?? ????????????else?if(v.getId()?==?R.id.text3){?? ????????????????showDetail(3);?? ????????????}?? ????????????else?if(v.getId()?==?R.id.text4){?? ????????????????showDetail(4);?? ????????????}?? ????????}?? ?????????? ????}?? }??
剩下的就是四個(gè)Fragment和他們的布局了。其實(shí)都是一樣的,我就只貼出FragmentInit和它對應(yīng)的布局文件fragment_init.xml
[java]?view plaincopy
package?com.jackchan.fragmenttest;?? ?? import?android.os.Bundle;?? import?android.app.Fragment;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.ViewGroup;?? ?? public?class?FragmentInit?extends?Fragment{?? ?????? ????@Override?? ????? ? ? ?? ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?? ????????????Bundle?savedInstanceState)?{?? ?????????? ?????????? ????????if(container?==?null){?? ????????????return?null;?? ????????}?? ????????View?view?=?inflater.inflate(R.layout.fragment_init,?container,?false);?? ????????return?view;?? ????}?? }??
[html]?view plaincopy
<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"??? ????>?? ????<TextView??? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:text="初始Fragment"?? ????????/>?? </LinearLayout>??
FragmentA,FragmentB,FragmentC我寫得差不多,差別在每個(gè)布局文件里的TextView的文字分別為這個(gè)是FragmentA,
這個(gè)是FragmentB,這個(gè)是FragmentC。
總結(jié)
以上是生活随笔為你收集整理的Android之Fragment使用简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。