當前位置:首頁 » 安卓系統 » android切換fragment

android切換fragment

發布時間: 2022-05-17 16:38:33

1. android中Fragment的切換方法。

需要做的准備
新建一個android項目,主Activity命名為MainActivity
創建三個Fragment1 Fragment2 Fragment3
為三個Fragment創建三個布局文fragment1.xml、fragment2.xml、fragment3.xml
布局 activity_main.xm
fragment1.xml樣例

Fragment1樣例

另外的可以根據樣例進行修改
activity_main.xml內容

activity打開時需要顯示一個fragment,也就是需要先向容器中添加一個fragment

這是就能顯示第二個fragment了,那麼如何切換不同的fragment呢,切換的過程如下
開啟事務
調用事務的replace方法,將當前容器的fragment替換為新的fragment
提交事務

注意上圖紅框中的addToBackStack方法,很多人都不是很清楚這個方法的實際作用,說一下程序運行時候的現象你就明白了
初始化時顯示的是 fragment2
實踐1 :fragment2------點擊按鈕frag1-----按返回鍵--------退出應用
實踐2: fragment2-----點擊按鈕frag3 -----按返回鍵--------返回到fragment2
為啥會出現上面的情況,原因就是切換到fragment3時,調用了addToBackStack方法,這時會將fragment2先入棧,然後再切換到fragment3,按返回鍵的時候fragment3銷毀,fragment2出棧顯示,而切換到fragme1時沒有將fragme2入棧,所以fragment2就直接銷毀了,再按返回鍵就直接退出應用了

2. 如何解決fragment之間切換時黑屏問題

Android中使用fragment來實現頁面的切換,如果導致fragment之間切換時黑屏,可能的原因如下:
Fragment所綁定的activity已經被銷毀,而程序引用了activity,報空指針就會意外退出。
Fragment新建之後,沒有進行commit操作,導致無法實例化該Fragment。
解決的方式是在使用activity時,進行判斷,如下代碼:
@Override
public void onHiddenChanged(boolean hidd) {
if (!hidd && getActivity() != null) {
System.out.println("是否執行了這個方法");
}
}

3. android viewpager如何替換fragment

ViewPager.setOffscreenPageLimit(x);//x表示預載入幾頁,這個應該可以解決你顯示空白的問題
然後你要有ViewPagerAdapter,
ViewPagerAdapter extends FragmentPagerAdapter
在重寫的getItem()里判斷return 不同的Fragment對象
加油,沒寫清楚的話可以去下個demo分析

4. 安卓fragmentactivity怎麼用切換

版本說明
fragment是在3.0版本引入的,但是也可以在以前的版本中使用,需要引入android-support-v4.jar支持包,當然調用的方法也不一樣,下面介紹的方法兼容了3.0以前的版本,主要介紹fragment的切換
需要做的准備
新建一個android項目,主Activity命名為MainActivity
創建三個Fragment1 Fragment2 Fragment3
為三個Fragment創建三個布局文fragment1.xml、fragment2.xml、fragment3.xml
布局 activity_main.xm
fragment1.xml樣例

Fragment1樣例

另外的可以根據樣例進行修改
activity_main.xml內容

activity打開時我們需要顯示一個fragment,也就是需要先向容器中添加一個fragment

這是就能顯示第二個fragment了,那麼如何切換不同的fragment呢,切換的過程如下
開啟事務
調用事務的replace方法,將當前容器的fragment替換為新的fragment
提交事務

注意上圖紅框中的addToBackStack方法,很多人都不是很清楚這個方法的實際作用,說一下程序運行時候的現象你就明白了
初始化時顯示的是 fragment2
實踐1 :fragment2------點擊按鈕frag1-----按返回鍵--------退出應用
實踐2: fragment2-----點擊按鈕frag3 -----按返回鍵--------返回到fragment2
為啥會出現上面的情況,原因就是切換到fragment3時,調用了addToBackStack方法,這時會將fragment2先入棧,然後再切換到fragment3,按返回鍵的時候fragment3銷毀,fragment2出棧顯示,而切換到fragme1時沒有將fragme2入棧,所以fragment2就直接銷毀了,再按返回鍵就直接退出應用了

5. android activity中有多個fragment切換

1、首先定義RegistActivity

public class RegistActivity extends Activity {

private EditText userEditText;
private EditText verifyCodeText;

private Fragment verifyCodeFragment;
private Fragment checkCodeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regist);

if (savedInstanceState == null) {
verifyCodeFragment = new VerifyCodeFragment();
getFragmentManager().beginTransaction()
.add(R.id.activity_regist, verifyCodeFragment).commit();
}
}
}
activity_regist.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas。android。com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_regist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.javen.activity.RegistActivity" >

</LinearLayout>
這邊通過java代碼來加入Fragment。
2、fragment_verifycode.xml 獲取驗證碼的界面


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas。android。com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.javen.activity.fragment.VerifyCodeFragment" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/message"
android:textSize="@dimen/label_font_size" />

<EditText
android:id="@+id/userEditText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="@dimen/label_font_size" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >

<Button
android:id="@+id/bnRegist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="verifyCodeListener"
android:text="@string/next" />
</LinearLayout>

</LinearLayout>
2、輸入驗證碼的界面:fragment_checkcode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.javen.activity.fragment.CheckCodeFragment" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/verifyCode"
android:textSize="@dimen/label_font_size" />

<EditText
android:id="@+id/verifyCodeText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >

<Button
android:id="@+id/bnRegist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="checkCodeListener"
android:text="@string/next" />
</LinearLayout>

</LinearLayout>
3、設置按鈕Listener,此處需要在Activity裡面添加方法。
public void verifyCodeListener(View source) {

userEditText = (EditText) verifyCodeFragment.getView().findViewById(
R.id.userEditText);

String phoneNumber = userEditText.getText().toString().trim();
if (!Tools.matchPhone(phoneNumber)) {<span style="white-space:pre"> </span>//對手機號碼驗證的一個正則表達式方法
DialogUtil.showDialog(this, Constant.LOGIN_USER_NAME, false);
return;
}
this.getVerifyCode(phoneNumber);<span style="white-space:pre"> </span>//Http請求獲取驗證碼
// 釋放當前的fragment,重新設置簡訊驗證碼輸入的fragment
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.remove(verifyCodeFragment);
checkCodeFragment = new CheckCodeFragment();
transaction.add(R.id.activity_regist, checkCodeFragment).commit();
}
主要切換代碼為:
<span style="white-space:pre"> </span>// 釋放當前的fragment,重新設置簡訊驗證碼輸入的fragment
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.remove(verifyCodeFragment);
checkCodeFragment = new CheckCodeFragment();
transaction.add(R.id.activity_regist, checkCodeFragment).commit();
checkCodeListener:
public void checkCodeListener(View source) {
verifyCodeText = (EditText) checkCodeFragment.getView().findViewById(
R.id.verifyCodeText);
String verifyCode = verifyCodeText.getText().toString().trim();

Map<String, String> params = new HashMap<String, String>();
params.put("verifyCode", verifyCode);

String url = UrlsUtil.formatUrl(UrlConstant.REGIST_CHECKCODE);

String result = null;
try {
result = HttpsUtil.postRequest(url, params);
} catch (Exception e) {
e.printStackTrace();
DialogUtil.showDialog(this, Constant.SERVICE_ERRO, false);
}

if(result != null){
//TODO
}
}
說明一下:
這邊主要說明的是Fragment切換的過程,至於Http請求,Util方法什麼的,只要了解這個功能即可,代碼其實和普通工具類似的。
轉載

6. android fragment相互切換的時候生命周期怎麼走

Fragment的生命周期初探:
因為Fragment必須嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相關的。
如果Activity是暫停狀態,其中所有的Fragment都是暫停狀態;如果Activity是stopped狀態,這個Activity中所有的Fragment都不能被啟動;如果Activity被銷毀,那麼它其中的所有Fragment都會被銷毀。
但是,當Activity在活動狀態,可以獨立控制Fragment的狀態,比如加上或者移除Fragment。
當這樣進行fragment transaction(轉換)的時候,可以把fragment放入Activity的back stack中,這樣用戶就可以進行返回操作。
使用Fragment時,需要繼承Fragment或者Fragment的子類(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),所以Fragment的代碼看起來和Activity的類似。
每當創建一個Fragment時,首先添加以下三個回調方法:
onCreate():系統在創建Fragment的時候調用這個方法,這里應該初始化相關的組件,一些即便是被暫停或者被停止時依然需要保留的東西。 onCreateView():當第一次繪制Fragment的UI時系統調用這個方法,該方法將返回一個View,如果Fragment不提供UI也可以返回null。注意,如果繼承自ListFragment,onCreateView()默認的實現會返回一個ListView,所以不用自己實現。 onPause():當用戶離開Fragment時第一個調用這個方法,需要提交一些變化,因為用戶很可能不再返回來。
將Fragment載入到Activity當中有兩種方式:
方式一:添加Fragment到Activity的布局文件當中 方式二:在Activity的代碼中動態添加Fragment(薦)
第一種方式雖然簡單但靈活性不夠。添加Fragment到Activity的布局文件當中,就等同於將Fragment及其視圖與activity的視圖綁定在一起,且在activity的生命周期過程中,無法切換fragment視圖。
第二種方式比較復雜,但也是唯一一種可以在運行時控制fragment的方式(載入、移除、替換)。

7. android怎麼設置fragment切換

需要做的准備
新建一個android項目,主Activity命名為MainActivity
創建三個Fragment1 Fragment2 Fragment3
為三個Fragment創建三個布局文fragment1.xml、fragment2.xml、fragment3.xml
布局 activity_main.xm
fragment1.xml樣例

Fragment1樣例

另外的可以根據樣例進行修改
activity_main.xml內容

activity打開時我們需要顯示一個fragment,也就是需要先向容器中添加一個fragment
這是就能顯示第二個fragment了,那麼如何切換不同的fragment呢,切換的過程如下
開啟事務
調用事務的replace方法,將當前容器的fragment替換為新的fragment
提交事務
初始化時顯示的是 fragment2
實踐1 :fragment2------點擊按鈕frag1-----按返回鍵--------退出應用
實踐2: fragment2-----點擊按鈕frag3 -----按返回鍵--------返回到fragment2
為啥會出現上面的情況,原因就是切換到fragment3時,調用了addToBackStack方法,這時會將fragment2先入棧,然後再切換到fragment3,按返回鍵的時候fragment3銷毀,fragment2出棧顯示,而切換到fragme1時沒有將fragme2入棧,所以fragment2就直接銷毀了,再按返回鍵就直接退出應用了。

8. Android中Fragment切換時,難道每次都要新創建Fragment實例嗎

你可以先把兩個Fragment添加進去(使用add而不是replace方法),然後再使用FragmentManager的show()和hide()來控制具體該顯示哪個。

9. android中在多個Fragment中切換是否沉浸,界面布局不能鋪滿或者被拉伸

天想試試沉浸欄的使用,但是按照網上相應的方法設置完成後,沒有達到想要的結果。使用情況是activity配合groupradio實現fragment切換,每個fragment的狀態欄需要顯示不同的顏色。通過hide和show的方式控制顯示當前fragment。在對應的xml中設置Android:fitsSystemWindows="true",以達到改變狀態欄顏色的問題(具體如何配置請查閱其他文檔)。

但是這樣做第一個fragment達到了想要的效果,其他三個狀態欄的顏色正確,但是padding沒有設置成功。標題欄佔用狀態欄的位置。

解決方法:

將xml中的android:fitsSystemWindows="true"去掉,通過代碼的方式來控制。在默認第一個fragment的onCreateView中設置view.setFitsSystemWindows(true);該view為該fragment對應的view。在onHiddenChanged中,設置對應的狀態。

詳細

熱點內容
android匹配 發布:2024-04-20 22:58:33 瀏覽:168
string的長度java 發布:2024-04-20 22:46:20 瀏覽:136
網易我的世界監獄風雲的伺服器 發布:2024-04-20 22:35:41 瀏覽:186
linux服務自動重啟 發布:2024-04-20 22:34:54 瀏覽:962
編譯器最後的結果 發布:2024-04-20 22:30:38 瀏覽:821
安裝linuxoracle11g 發布:2024-04-20 22:29:02 瀏覽:533
android設置權重 發布:2024-04-20 22:20:08 瀏覽:725
什麼手機安卓系統80 發布:2024-04-20 21:37:29 瀏覽:380
浙江萬里的伺服器地址 發布:2024-04-20 21:16:59 瀏覽:408
ndklinux下載 發布:2024-04-20 21:05:22 瀏覽:567