最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法

 更新時(shí)間:2015年10月30日 14:47:42   作者:_YMW  
這篇文章主要介紹了Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Fragment界面跳轉(zhuǎn)所涉及的相關(guān)知識(shí)點(diǎn)與實(shí)現(xiàn)技巧,并附帶了完整的實(shí)例代碼供讀者下載參考,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • 利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法

    利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Android自帶的四種線程池使用總結(jié)

    Android自帶的四種線程池使用總結(jié)

    本篇文章主要介紹了Android自帶的四種線程池使用總結(jié),詳細(xì)的介紹了4種線程池的用法,具有一定的參考價(jià)值,有興趣的小伙伴可以了解一下
    2017-07-07
  • Android編程之SDK安裝組件的離線安裝方法分享

    Android編程之SDK安裝組件的離線安裝方法分享

    這篇文章主要介紹了Android編程之SDK安裝組件的離線安裝方法,提供了離線sdk的下載地址與下載方法,以及離線安裝的具體操作步驟,需要的朋友可以參考下
    2015-12-12
  • android命令行模擬輸入事件(文字、按鍵、觸摸等)

    android命令行模擬輸入事件(文字、按鍵、觸摸等)

    這篇文章主要給大家介紹了關(guān)于android命令行模擬輸入事件,例如文字、按鍵、觸摸等的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android開(kāi)發(fā)環(huán)境搭建圖文教程 親測(cè)有效!

    Android開(kāi)發(fā)環(huán)境搭建圖文教程 親測(cè)有效!

    這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)環(huán)境搭建圖文教程,親自測(cè)試有效的搭建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android Listview點(diǎn)贊問(wèn)題關(guān)于圖片重復(fù)問(wèn)題

    Android Listview點(diǎn)贊問(wèn)題關(guān)于圖片重復(fù)問(wèn)題

    最近在開(kāi)發(fā)android方面的項(xiàng)目時(shí),遇到很多問(wèn)題,下面小編以listview 與 baseadapter結(jié)合使用為例,給大家分享下關(guān)于點(diǎn)贊的的時(shí)候 圖片重復(fù)問(wèn)題的解決方法,一起看看吧
    2016-11-11
  • Android編程自定義AlertDialog樣式的方法詳解

    Android編程自定義AlertDialog樣式的方法詳解

    這篇文章主要介紹了Android編程自定義AlertDialog樣式的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義AlertDialog樣式的具體布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • Kotlin 與 Jetpack Compose 參數(shù)設(shè)計(jì)完全指南(最新推薦)

    Kotlin 與 Jetpack Compose 參數(shù)設(shè)計(jì)完全指南(最新推薦)

    作為 Kotlin 和 Jetpack Compose 開(kāi)發(fā)者,合理的參數(shù)設(shè)計(jì)能顯著提升代碼的可讀性和易用性,本文將系統(tǒng)整理各類參數(shù)規(guī)則,幫助您編寫(xiě)更優(yōu)雅的 API,感興趣的朋友一起看看吧
    2025-04-04
  • 簡(jiǎn)單談?wù)勎业腁ndroid屏幕適配之路

    簡(jiǎn)單談?wù)勎业腁ndroid屏幕適配之路

    我相信Android碎片化問(wèn)題是讓所有的Android開(kāi)發(fā)者都比較頭疼的問(wèn)題.尤其是屏幕適配這一塊兒.想要自己的app在不同的設(shè)備上面都有一個(gè)比較好的顯示效果.就必須做好相應(yīng)的屏幕適配.
    2017-11-11
  • ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁(yè)

    ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁(yè)

    這篇文章主要為大家詳細(xì)介紹了ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09

最新評(píng)論

利川市| 睢宁县| 乡城县| 赤水市| 汉阴县| 永春县| 西宁市| 西华县| 固原市| 太湖县| 贡觉县| 虎林市| 咸丰县| 禄丰县| 朝阳区| 芜湖市| 雅安市| 马公市| 化德县| 衢州市| 获嘉县| 乌鲁木齐县| 禄丰县| 银川市| 昭苏县| 台湾省| 蒲城县| 溧阳市| 庆安县| 阜南县| 安吉县| 明水县| 突泉县| 紫云| 新晃| 庄浪县| 边坝县| 夹江县| 罗山县| 延吉市| 铁岭市|