溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Fragment基本使用

發(fā)布時(shí)間:2020-07-24 20:30:46 來(lái)源:網(wǎng)絡(luò) 閱讀:390 作者:671076656 欄目:移動(dòng)開發(fā)


Android是在Android 3.0 (API level 11)開始引入Fragment的。

  可以把Fragment想成Activity中的模塊,這個(gè)模塊有自己的布局,有自己的生命周期,單獨(dú)處理自己的輸入,在Activity運(yùn)行的時(shí)候可以加載或者移除Fragment模塊。

  可以把Fragment設(shè)計(jì)成可以在多個(gè)Activity中復(fù)用的模塊。

  當(dāng)開發(fā)的應(yīng)用程序同時(shí)適用于平板電腦和手機(jī)時(shí),可以利用Fragment實(shí)現(xiàn)靈活的布局,改善用戶體驗(yàn)。


顯示類似于tabhost,可以切換界面

private FirstFragment firstFragment;
firstFragment = new FirstFragment();

 

private void setDefaultFragment(){  

        FragmentManager fm = getFragmentManager();  
        FragmentTransaction transaction = fm.beginTransaction();  
        transaction.replace(R.id.content, firstFragment);  
        transaction.show(firstFragment);
        transaction.commit();  
}
Fragment所使用的佈局
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


//自定義一個(gè)FirstFragment繼承Fragment

public class FirstFragment extends Fragment implements OnClickListener{

private Button btn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,                   Bundle savedInstanceState)
{

  View view = inflater.inflate(R.layout.layout_first, container, false);
  btn = (Button) view.findViewById(R.id.button1);
  btn.setOnClickListener(this);
  
  return view;
}

public void onResume(){
  super.onResume();
}

@Override
public void onClick(View arg0) {
  if(arg0.getId() == R.id.button1){
    Toast.makeText(getActivity(), "click", 200).show();
  }
 }
}


//關(guān)於Fragment傳值

//可以傳遞數(shù)組、對(duì)象等

        Bundle bundle = new Bundle();
        bundle.putString("name", "阿三");
        firstFragment.setArguments(bundle);

	FragmentManager fm = getFragmentManager();  
	FragmentTransaction transaction = fm.beginTransaction();  
	transaction.replace(R.id.content, firstFragment);  
	transaction.show(firstFragment);
	transaction.commit();


//接收傳值

  String name = getArguments().getString("name");


如何切換Fragment顯示不同界面

        FragmentManager fm = getFragmentManager();  
        FragmentTransaction transactio
        if(arg1 == true){
            transaction.replace(R.id.content, firstFragment); 
        }else{
            transaction.replace(R.id.content, secondFragment); 
        }
        
        transaction.commit();


Fragment家族常用的API

Fragment常用的三個(gè)類:

android.app.Fragment 主要用于定義Fragment

android.app.FragmentManager 主要用于在Activity中操作Fragment

android.app.FragmentTransaction 保證一些列Fragment操作的原子性,熟悉事務(wù)這個(gè)詞,一定能明白~

a、獲取FragmentManage的方式:

getFragmentManager() // v4中,getSupportFragmentManager

b、主要的操作都是FragmentTransaction的方法

FragmentTransaction transaction = fm.benginTransatcion();//開啟一個(gè)事務(wù)

transaction.add() 

往Activity中添加一個(gè)Fragment

transaction.remove()

從Activity中移除一個(gè)Fragment,如果被移除的Fragment沒有添加到回退棧,這個(gè)Fragment實(shí)例將會(huì)被銷毀。

transaction.replace()

使用另一個(gè)Fragment替換當(dāng)前的,實(shí)際上就是remove()然后add()的合體~

transaction.hide()

隱藏當(dāng)前的Fragment,僅僅是設(shè)為不可見,并不會(huì)銷毀

transaction.show()

顯示之前隱藏的Fragment

detach()

會(huì)將view從UI中移除,和remove()不同,此時(shí)fragment的狀態(tài)依然由FragmentManager維護(hù)。

attach()

重建view視圖,附加到UI上并顯示。

transatcion.commit()//提交一個(gè)事務(wù)

注意:常用Fragment的哥們,可能會(huì)經(jīng)常遇到這樣Activity狀態(tài)不一致:State loss這樣的錯(cuò)誤。主要是因?yàn)椋篶ommit方法一定要在Activity.onSaveInstance()之前調(diào)用。

上述,基本是操作Fragment的所有的方式了,在一個(gè)事務(wù)開啟到提交可以進(jìn)行多個(gè)的添加、移除、替換等操作。

值得注意的是:如果你喜歡使用Fragment,一定要清楚這些方法,哪個(gè)會(huì)銷毀視圖,哪個(gè)會(huì)銷毀實(shí)例,哪個(gè)僅僅只是隱藏,這樣才能更好的使用它們。

a、比如:我在FragmentA中的EditText填了一些數(shù)據(jù),當(dāng)切換到FragmentB時(shí),如果希望會(huì)到A還能看到數(shù)據(jù),則適合你的就是hide和show;也就是說(shuō),希望保留用戶操作的面板,你可以使用hide和show,當(dāng)然了不要使勁在那new實(shí)例,進(jìn)行下非null判斷。

b、再比如:我不希望保留用戶操作,你可以使用remove(),然后add();或者使用replace()這個(gè)和remove,add是相同的效果。

c、remove和detach有一點(diǎn)細(xì)微的區(qū)別,在不考慮回退棧的情況下,remove會(huì)銷毀整個(gè)Fragment實(shí)例,而detach則只是銷毀其視圖結(jié)構(gòu),實(shí)例并不會(huì)被銷毀。那么二者怎么取舍使用呢?如果你的當(dāng)前Activity一直存在,那么在不希望保留用戶操作的時(shí)候,你可以優(yōu)先使用detach。


部分轉(zhuǎn)自:http://blog.csdn.net/lmj623565791/article/details/37992017



fragment接收activity的回調(diào)處理

// 回調(diào)函數(shù)

//import android.app.Fragment;

//需要導(dǎo)入.app這個(gè)包,V4包測(cè)試收不到回調(diào)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(data == null){

        return;
    }
}


//*布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:gravity="center" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <View
            android:id="@+id/layout_order_top"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="@color/main_color"
            android:visibility="gone" />
        <LinearLayout
            android:id="@+id/ll_order_title"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@id/layout_order_top"
            android:background="@color/main_color"
            android:visibility="gone" >
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:gravity="center" >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/shape_radius_order_search"
                    android:gravity="center"
                    android:orientation="horizontal" >
                    <com.zhiduan.crowdclient.view.ClearEditText
                        android:id="@+id/edt_order_billcode"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:background="#00000000"
                        android:digits="@string/edit_digits"
                        android:drawableLeft="@drawable/order_magnifier"
                        android:drawableRight="@drawable/homepage_close"
                        android:gravity="center_vertical"
                        android:hint=" 輸掃描運(yùn)單號(hào)"
                        android:imeOptions="actionSearch"
                        android:maxLength="20"
                        android:singleLine="true"
                        android:textSize="12sp" />
                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="8dp"
                        android:background="@color/main_bg_color" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="12dp"
                        android:onClick="scan"
                        android:src="@drawable/order_scancode" />
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:onClick="orderSort"
                android:padding="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="20dp" >
                <ImageView
                    android:id="@+id/p_w_picpathView_sort"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/order_screen" />
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_order_menu"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/ll_order_title"
            android:layout_marginTop="3dp"
            android:orientation="vertical" >
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />
            <FrameLayout
                android:id="@+id/fl_bottom_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff" >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="horizontal" >
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical" >
                        <TextView
                            android:id="@+id/tv_order_wait"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:gravity="center"
                            android:onClick="waitTaking"
                            android:text="待取件(0)"
                            android:textColor="@color/main_color"
                            android:textSize="16dp" />
                        <View
                            android:id="@+id/view_wait"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/main_color" />
                    </LinearLayout>
                    <View
                        android:layout_width="0.1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="10dp"
                        android:background="@color/gray_4" />
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical" >
                        <TextView
                            android:id="@+id/tv_order_sending"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:gravity="center"
                            android:onClick="distribution"
                            android:text="配送中(0)"
                            android:textColor="#212124"
                            android:textSize="16dp" />
                        <View
                            android:id="@+id/view_sending"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/transparent" />
                    </LinearLayout>
                    <View
                        android:layout_width="0.1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="10dp"
                        android:background="@color/gray_4" />
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical" >
                        <TextView
                            android:id="@+id/tv_order_signed"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:gravity="center"
                            android:onClick="signed"
                            android:text="已簽收(0)"
                            android:textColor="#212124"
                            android:textSize="16dp" />
                        <View
                            android:id="@+id/view_signed"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/transparent" />
                    </LinearLayout>
                    <View
                        android:layout_width="0.1dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="10dp"
                        android:background="@color/gray_4" />
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical" >
                        <TextView
                            android:id="@+id/tv_order_abnormal"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:gravity="center"
                            android:onClick="abNormal"
                            android:text="異常件(0)"
                            android:textColor="#212124"
                            android:textSize="16dp" />
                        <View
                            android:id="@+id/view_abnormal"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/transparent" />
                    </LinearLayout>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
        <!-- Tab 內(nèi)容 -->
        <LinearLayout
            android:id="@+id/ll_order_content"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/ll_order_menu"
            android:orientation="vertical" >
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="gone"
                android:background="#ffffff" />
            <FrameLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <!-- Tab按鈕 -->
    </RelativeLayout>
</TabHost>










向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI