91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Fragment基本使用

發布時間:2020-07-24 20:30:46 來源:網絡 閱讀:390 作者:671076656 欄目:移動開發


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

  可以把Fragment想成Activity中的模塊,這個模塊有自己的布局,有自己的生命周期,單獨處理自己的輸入,在Activity運行的時候可以加載或者移除Fragment模塊。

  可以把Fragment設計成可以在多個Activity中復用的模塊。

  當開發的應用程序同時適用于平板電腦和手機時,可以利用Fragment實現靈活的布局,改善用戶體驗。


顯示類似于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" />


//自定義一個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();
  }
 }
}


//關於Fragment傳值

//可以傳遞數組、對象等

        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常用的三個類:

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

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

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

a、獲取FragmentManage的方式:

getFragmentManager() // v4中,getSupportFragmentManager

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

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

transaction.add() 

往Activity中添加一個Fragment

transaction.remove()

從Activity中移除一個Fragment,如果被移除的Fragment沒有添加到回退棧,這個Fragment實例將會被銷毀。

transaction.replace()

使用另一個Fragment替換當前的,實際上就是remove()然后add()的合體~

transaction.hide()

隱藏當前的Fragment,僅僅是設為不可見,并不會銷毀

transaction.show()

顯示之前隱藏的Fragment

detach()

會將view從UI中移除,和remove()不同,此時fragment的狀態依然由FragmentManager維護。

attach()

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

transatcion.commit()//提交一個事務

注意:常用Fragment的哥們,可能會經常遇到這樣Activity狀態不一致:State loss這樣的錯誤。主要是因為:commit方法一定要在Activity.onSaveInstance()之前調用。

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

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

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

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

c、remove和detach有一點細微的區別,在不考慮回退棧的情況下,remove會銷毀整個Fragment實例,而detach則只是銷毀其視圖結構,實例并不會被銷毀。那么二者怎么取舍使用呢?如果你的當前Activity一直存在,那么在不希望保留用戶操作的時候,你可以優先使用detach。


部分轉自:http://blog.csdn.net/lmj623565791/article/details/37992017



fragment接收activity的回調處理

// 回調函數

//import android.app.Fragment;

//需要導入.app這個包,V4包測試收不到回調

@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=" 輸掃描運單號"
                        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 內容 -->
        <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問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

鄂温| 射阳县| 光泽县| 宽甸| 尼玛县| 梅河口市| 海门市| 龙泉市| 十堰市| 民县| 华坪县| 临沂市| 弋阳县| 航空| 南华县| 织金县| 彩票| 玉山县| 安西县| 古田县| 通江县| 合川市| 伊宁县| 泌阳县| 禄丰县| 泽州县| 东港市| 烟台市| 莱芜市| 钟祥市| 中山市| 沙湾县| 曲松县| 三门峡市| 读书| 西畴县| 梨树县| 新竹市| 酉阳| 乐安县| 精河县|