欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 管理Activity中的fragments

 更新時間:2013年01月10日 09:42:46   作者:  
為了管理Activity中的fragments,需要使用FragmentManager,為了得到它,需要調(diào)用Activity中的getFragmentManager()方法,接下來詳細介紹,感興趣的朋友可以了解下哦
FragmentManager
為了管理Activity中的fragments,需要使用FragmentManager.
為了得到它,需要調(diào)用Activity中的getFragmentManager()方法。
因為FragmentManager的API是在Android 3.0,也即API level 11開始引入的,所以對于之前的版本,需要使用support library中的FragmentActivity,并且使用getSupportFragmentManager()方法。
用FragmentManager可以做的工作有:
得到Activity中存在的fragment
使用findFragmentById()或findFragmentByTag()方法。
將fragment彈出back stack
popBackStack():將back stack中最后一次的fragment轉(zhuǎn)換彈出。如果沒有可以出棧的東西,返回false。
這個函數(shù)是異步的:它將彈出棧的請求加入隊列,但是這個動作直到應用回到事件循環(huán)才會執(zhí)行。
為back stack加上監(jiān)聽器
addOnBackStackChangedListener()
Performing Fragment Transactions
使用Fragment時,可以通過用戶交互來執(zhí)行一些動作,比如增加、移除、替換等。
所有這些改變構(gòu)成一個集合,這個集合被叫做一個transaction。
可以調(diào)用FragmentTransaction中的方法來處理這個transaction,并且可以將transaction存進由activity管理的back stack中,這樣用戶就可以進行fragment變化的回退操作。
可以這樣得到FragmentTransaction類的實例: 
復制代碼 代碼如下:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

每個transaction是一組同時執(zhí)行的變化的集合。
用add(), remove(), replace()方法,把所有需要的變化加進去,然后調(diào)用commit()方法,將這些變化應用。
在commit()方法之前,你可以調(diào)用addToBackStack(),把這個transaction加入back stack中去,這個back stack是由activity管理的,當用戶按返回鍵時,就會回到上一個fragment的狀態(tài)。
比如下面的代碼就是用一個新的fragment取代之前的fragment,并且將前次的狀態(tài)存儲在back stack中。
復制代碼 代碼如下:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();

在這個例子中,newFragment將取代在R.id.fragment_container容器中的fragment,如果沒有,將直接添加新的fragment。

通過調(diào)用addToBackStack(),commit()的一系列轉(zhuǎn)換作為一個transaction被存儲在back stack中,用戶按Back鍵可以返回上一個轉(zhuǎn)換前的狀態(tài)。

當你移除一個fragment的時候,如果commit()之前沒有調(diào)用addToBackStack(),那個fragment將會是destroyed;如果調(diào)用了addToBackStack(),這個fragment會是stopped,可以通過返回鍵來恢復。
關(guān)于commit()方法
調(diào)用commit()方法并不能立即執(zhí)行transaction中包含的改變動作,commit()方法把transaction加入activity的UI線程隊列中。

但是,如果覺得有必要的話,可以調(diào)用executePendingTransactions()方法來立即執(zhí)行commit()提供的transaction。
這樣做通常是沒有必要的,除非這個transaction被其他線程依賴。
注意:你只能在activity存儲它的狀態(tài)(當用戶要離開activity時)之前調(diào)用commit(),如果在存儲狀態(tài)之后調(diào)用commit(),將會拋出一個異常。

這是因為當activity再次被恢復時commit之后的狀態(tài)將丟失。如果丟失也沒關(guān)系,那么使用commitAllowingStateLoss()方法。
實例程序
寫了個小程序?qū)嵺`了一下fragment的管理,程序不是很完善,就是試試基本用法,先按第一個按鈕添加一個fragment,第二個按鈕將其替換,第三個按鈕將第二個按鈕添加的fragment刪除。
相關(guān)代碼:
第一個fragment
復制代碼 代碼如下:

ExampleFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExampleFragment extends Fragment
{
//三個一般必須重載的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("ExampleFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("ExampleFragment--onCreateView");
return inflater.inflate(R.layout.example_fragment_layout, container, false);
}
@Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("ExampleFragment--onPause");
}
@Override
public void onResume()
{
// TODO Auto-generated method stub
super.onResume();
System.out.println("ExampleFragment--onResume");
}
@Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
System.out.println("ExampleFragment--onStop");
}
}

它的布局
復制代碼 代碼如下:

example_fragment_layout.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="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="20dip"
android:text="@string/fragment1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2"
/>
</LinearLayout>

第二個fragment:
復制代碼 代碼如下:

NewFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NewFragment extends Fragment
{
//三個一般必須重載的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("NewFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("NewFragment--onCreateView");
return inflater.inflate(R.layout.new_fragment_layout, container, false);
}
@Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("NewFragment--onPause");
}
}

復制代碼 代碼如下:

new_fragment_layout.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="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:text="@string/fragment2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num3"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num4"
/>
</LinearLayout>

主Activity
復制代碼 代碼如下:

LearnFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class LearnFragment extends FragmentActivity
{
Button btn1;
Button btn2;
Button btn3;
ExampleFragment fragmentE;
NewFragment fragmentN;
FragmentManager fragmentManager;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn_fragment);
findViews();
setListeners();
//獲得Fragment管理所需要的類的對象
fragmentManager = getSupportFragmentManager();
}
private void findViews()
{
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
}
private void setListeners()
{
//第一個按鈕,增加一個ExampleFragment
btn1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//在程序中加入ExampleFragment
fragmentE = new ExampleFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.linear1,fragmentE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
//第二個按鈕,用一個NewFragment替換前面增加的那個fragment
btn2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
fragmentN = new NewFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear1,fragmentN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
//第三個按鈕,移除fragment
btn3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
}
}

復制代碼 代碼如下:

activity_learn_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout1"
/>
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"
/>
<fragment
android:name="com.example.learningfragment.ExampleFragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3"
/>
</LinearLayout>
</LinearLayout>

資源
復制代碼 代碼如下:

strings.xml
<resources>
<string name="app_name">LearningFragment</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_learn_fragment">LearnFragment</string>
<string name="layout1">LinearLayout1</string>
<string name="layout2">LinearLayout2</string>
<string name="fragment1">FragmentType1</string>
<string name="fragment2">FragmentType2</string>
<string name="num1">NO.1</string>
<string name="num2">NO.2</string>
<string name="num3">NO.3</string>
<string name="num4">NO.4</string>
<string name="btn1">Add fragment</string>
<string name="btn2">Replace fragment</string>
<string name="btn3">Remove fragment</string>
</resources>

程序運行截圖
 
參考資料
API Guides:Fragments
http://developer.android.com/guide/components/fragments.html
FragmentManager類文檔:
http://developer.android.com/reference/android/app/FragmentManager.html
FragmentTransaction類文檔
http://developer.android.com/reference/android/app/FragmentTransaction.html

相關(guān)文章

  • Android 保存Fragment 切換狀態(tài)實例代碼

    Android 保存Fragment 切換狀態(tài)實例代碼

    本文主要介紹Android Fragment的應用,這里給大家用實例代碼詳細介紹了Android Fragment 切換狀態(tài),有需要的小伙伴可以參考下
    2016-07-07
  • 模擬按Home鍵退出應用的簡單方法(分享)

    模擬按Home鍵退出應用的簡單方法(分享)

    下面小編就為大家?guī)硪黄M按Home鍵退出應用的簡單方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android編程實現(xiàn)activity dialog透明背景的方法

    Android編程實現(xiàn)activity dialog透明背景的方法

    這篇文章主要介紹了Android編程實現(xiàn)activity dialog透明背景的方法,涉及Activity相關(guān)屬性設置及配置文件操作技巧,需要的朋友可以參考下
    2017-07-07
  • 一篇文章弄懂kotlin的擴展方法

    一篇文章弄懂kotlin的擴展方法

    這篇文章主要給大家介紹了如何通過一篇文章弄懂kotlin的擴展方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • Android調(diào)用外置攝像頭的方法

    Android調(diào)用外置攝像頭的方法

    這篇文章主要為大家詳細介紹了Android調(diào)用外置攝像頭的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Kotlin?object的幾種用法示例詳解

    Kotlin?object的幾種用法示例詳解

    這篇文章主要為大家介紹了Kotlin?object的幾種用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android實現(xiàn)動態(tài)添加數(shù)據(jù)與堆疊折線圖詳解流程

    Android實現(xiàn)動態(tài)添加數(shù)據(jù)與堆疊折線圖詳解流程

    堆疊折線圖是折線圖的一種,堆積折線圖用于顯示每一數(shù)值所占大小隨時間或有序類別而變化的趨勢,可能顯示數(shù)據(jù)點以表示單個數(shù)據(jù)值,也可能不顯示這些數(shù)據(jù)點。堆疊折線圖中,類別數(shù)據(jù)沿水平軸均勻分布,所有值數(shù)據(jù)沿垂直軸均勻分布
    2021-10-10
  • Flutter自定義Appbar搜索框效果

    Flutter自定義Appbar搜索框效果

    這篇文章主要為大家詳細介紹了Flutter自定義Appbar搜索框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 詳解Flutter中Dart集合使用教程

    詳解Flutter中Dart集合使用教程

    集合是應用程序中最為常見的數(shù)據(jù)結(jié)構(gòu),Dart一共支持四種集合,其中核心的List,?Map和Set在基礎框架中。本文將詳細講解Dart集合的最佳實踐,需要的可以參考一下
    2022-05-05
  • Android使用Canvas繪制圓形進度條效果

    Android使用Canvas繪制圓形進度條效果

    這篇文章主要為大家詳細介紹了Android使用Canvas繪制圓形進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10

最新評論