Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法
本文實(shí)例講述了Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
1.首先貼上項(xiàng)目結(jié)構(gòu)圖:

2.先添加一個(gè)接口文件BackHandledInterface.java,定義一個(gè)setSelectedFragment方法用于設(shè)置當(dāng)前加載的Fragment在棧頂,主界面MainActivity須實(shí)現(xiàn)此接口,代碼如下:
package com.example.testdemo;
public interface BackHandledInterface {
public abstract void setSelectedFragment(BackHandledFragment selectedFragment);
}
3.定義一個(gè)抽象類BackHandledFragment繼承自Fragment,后面跳轉(zhuǎn)的Fragment界面都要繼承自BackHandledFragment。抽象類BackHandledFragment中定義一個(gè)返回值為boolean類型的onBackPressed方法,用于處理點(diǎn)擊返回按鍵(物理Back鍵)時(shí)的邏輯,若該方法返回false,表示當(dāng)前Fragment不消費(fèi)返回事件,而由Fragment所屬的FragmentActivity來(lái)處理這個(gè)事件。代碼如下:
package com.example.testdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public abstract class BackHandledFragment extends Fragment {
protected BackHandledInterface mBackHandledInterface;
/**
* 所有繼承BackHandledFragment的子類都將在這個(gè)方法中實(shí)現(xiàn)物理Back鍵按下后的邏輯
*/
protected abstract boolean onBackPressed();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!(getActivity() instanceof BackHandledInterface)) {
throw new ClassCastException(
"Hosting Activity must implement BackHandledInterface");
} else {
this.mBackHandledInterface = (BackHandledInterface) getActivity();
}
}
@Override
public void onStart() {
super.onStart();
// 告訴FragmentActivity,當(dāng)前Fragment在棧頂
mBackHandledInterface.setSelectedFragment(this);
}
}
4.主界面MainActivity要繼承FragmentActivity才能調(diào)用getSupportFragmentManager()方法來(lái)處理Fragment。MainActivity還需重寫(xiě)onBackPressed方法用來(lái)捕捉返回鍵(Back Key)事件,代碼如下:
package com.example.testdemo;
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.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements
BackHandledInterface {
private static MainActivity mInstance;
private BackHandledFragment mBackHandedFragment;
private Button btnSecond;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSecond = (Button) findViewById(R.id.btnSecond);
btnSecond.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FirstFragment first = new FirstFragment();
loadFragment(first);
btnSecond.setVisibility(View.GONE);
}
});
}
public static MainActivity getInstance() {
if (mInstance == null) {
mInstance = new MainActivity();
}
return mInstance;
}
public void loadFragment(BackHandledFragment fragment) {
BackHandledFragment second = fragment;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.firstFragment, second, "other");
ft.addToBackStack("tag");
ft.commit();
}
@Override
public void setSelectedFragment(BackHandledFragment selectedFragment) {
this.mBackHandedFragment = selectedFragment;
}
@Override
public void onBackPressed() {
if (mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()) {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
btnSecond.setVisibility(View.VISIBLE);
}
getSupportFragmentManager().popBackStack();
}
}
}
}
5.分別添加兩個(gè)子級(jí)Fragment,F(xiàn)irstFragment.java和SecondFragment.java,代碼分別如下:
FirstFragment.java:
package com.example.testdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class FirstFragment extends BackHandledFragment {
private View myView;
private Button btnSecond;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_first, null);
initView();
return myView;
}
private void initView() {
btnSecond = (Button) myView.findViewById(R.id.btnSecond);
btnSecond.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SecondFragment second = new SecondFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.firstFragment, second);
ft.addToBackStack("tag");
ft.commit();
}
});
}
@Override
protected boolean onBackPressed() {
return false;
}
}
SecondFragment.java:
package com.example.testdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends BackHandledFragment {
private View mView;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_second, null);
return mView;
}
@Override
protected boolean onBackPressed() {
return false;
}
}
6.三個(gè)布局文件代碼如下:
activity_main.xml:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FragmentActivity 父界面"
android:textSize="26sp" />
<Button
android:id="@+id/btnSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="跳轉(zhuǎn)到FirstFragment" />
<FrameLayout
android:id="@+id/firstFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
fragment_first.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FirstFragment"
android:textColor="#000000"
android:textSize="26sp" />
<Button
android:id="@+id/btnSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="打開(kāi)SecondFragment" />
</RelativeLayout>
fragment_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="SecondFragment"
android:textColor="#000000"
android:textSize="26sp" />
</RelativeLayout>
7.最后奉上實(shí)例鏈接:
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
- Android 6.0動(dòng)態(tài)權(quán)限及跳轉(zhuǎn)GPS設(shè)置界面的方法
- android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例
- Android如何通過(guò)scheme跳轉(zhuǎn)界面
- Android中檢查網(wǎng)絡(luò)連接狀態(tài)的變化無(wú)網(wǎng)絡(luò)時(shí)跳轉(zhuǎn)到設(shè)置界面
- Android 中按home鍵和跳轉(zhuǎn)到主界面的實(shí)例代碼
- Android跳轉(zhuǎn)到系統(tǒng)聯(lián)系人及撥號(hào)或短信界面
- Android中應(yīng)用界面主題Theme使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
- Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
相關(guān)文章
Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例
這篇文章主要介紹了Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android加載大分辨率圖片到手機(jī)內(nèi)存中的實(shí)例方法
有些圖片的分辨率比較高,把它直接加載到手機(jī)內(nèi)存中之后,會(huì)導(dǎo)致堆內(nèi)存溢出的問(wèn)題,下面就講解一下Android的堆內(nèi)存以及如何在Android應(yīng)用中加載一個(gè)高分辨率的圖片的方法2013-11-11
Android調(diào)用OpenCV2.4.10實(shí)現(xiàn)二維碼區(qū)域定位
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實(shí)現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView
這篇文章主要介紹了Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android如何實(shí)現(xiàn)鎖屏狀態(tài)下彈窗
在鎖屏狀態(tài)下彈窗的效果我們平時(shí)并不少見(jiàn),如QQ、微信和鬧鐘等,但是Android開(kāi)發(fā)者要怎么實(shí)現(xiàn)這一功能呢?下面一起來(lái)看看。2016-08-08
Android開(kāi)發(fā)案例手冊(cè)Application跳出dialog
這篇文章主要為大家介紹了Android開(kāi)發(fā)案例手冊(cè)Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼
本篇文章主要介紹了Android實(shí)現(xiàn)定時(shí)器Timer的停止和重啟實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android 中 android.view.WindowLeaked的解決辦法
這篇文章主要介紹了Android 中 android.view.WindowLeaked的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
android Tween Animation屬性設(shè)置方法實(shí)例
在Android開(kāi)發(fā)中,Animation是用來(lái)給控件制作效果的,本文介紹二種實(shí)現(xiàn)方法2013-11-11

