<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Android 使用Fragment界面向下跳轉并一級級返回

      1.首先貼上項目結構圖:

      2.先添加一個接口文件BackHandledInterface.java,定義一個setSelectedFragment方法用于設置當前加載的Fragment在棧頂,主界面MainActivity須實現此接口,代碼如下:

      package com.example.testdemo;
      
      public interface BackHandledInterface {
      
          public abstract void setSelectedFragment(BackHandledFragment selectedFragment);
      }

      3.定義一個抽象類BackHandledFragment繼承自Fragment,后面跳轉的Fragment界面都要繼承自BackHandledFragment。抽象類BackHandledFragment中定義一個返回值為boolean類型的onBackPressed方法,用于處理點擊返回按鍵(物理Back鍵)時的邏輯,若該方法返回false,表示當前Fragment不消費返回事件,而由Fragment所屬的FragmentActivity來處理這個事件。代碼如下:

       1 package com.example.testdemo;
       2 
       3 import android.os.Bundle;
       4 import android.support.v4.app.Fragment;
       5 
       6 public abstract class BackHandledFragment extends Fragment {
       7 
       8     protected BackHandledInterface mBackHandledInterface;
       9 
      10     /**
      11      * 所有繼承BackHandledFragment的子類都將在這個方法中實現物理Back鍵按下后的邏輯
      12      */
      13     protected abstract boolean onBackPressed();
      14 
      15     @Override
      16     public void onCreate(Bundle savedInstanceState) {
      17         super.onCreate(savedInstanceState);
      18         if (!(getActivity() instanceof BackHandledInterface)) {
      19             throw new ClassCastException(
      20                     "Hosting Activity must implement BackHandledInterface");
      21         } else {
      22             this.mBackHandledInterface = (BackHandledInterface) getActivity();
      23         }
      24     }
      25 
      26     @Override
      27     public void onStart() {
      28         super.onStart();
      29         // 告訴FragmentActivity,當前Fragment在棧頂
      30         mBackHandledInterface.setSelectedFragment(this);
      31     }
      32 
      33 }

      4.主界面MainActivity要繼承FragmentActivity才能調用getSupportFragmentManager()方法來處理Fragment。MainActivity還需重寫onBackPressed方法用來捕捉返回鍵(Back Key)事件,代碼如下:

       1 package com.example.testdemo;
       2 
       3 import android.os.Bundle;
       4 import android.support.v4.app.FragmentActivity;
       5 import android.support.v4.app.FragmentManager;
       6 import android.support.v4.app.FragmentTransaction;
       7 import android.view.View;
       8 import android.view.View.OnClickListener;
       9 import android.widget.Button;
      10 
      11 public class MainActivity extends FragmentActivity implements
      12         BackHandledInterface {
      13     private static MainActivity mInstance;
      14     private BackHandledFragment mBackHandedFragment;
      15     private Button btnSecond;
      16 
      17     @Override
      18     public void onCreate(Bundle savedInstanceState) {
      19         super.onCreate(savedInstanceState);
      20         setContentView(R.layout.activity_main);
      21         btnSecond = (Button) findViewById(R.id.btnSecond);
      22         btnSecond.setOnClickListener(new OnClickListener() {
      23 
      24             @Override
      25             public void onClick(View v) {
      26                 FirstFragment first = new FirstFragment();
      27                 loadFragment(first);
      28                 btnSecond.setVisibility(View.GONE);
      29             }
      30         });
      31 
      32     }
      33 
      34     public static MainActivity getInstance() {
      35         if (mInstance == null) {
      36             mInstance = new MainActivity();
      37         }
      38         return mInstance;
      39     }
      40 
      41     public void loadFragment(BackHandledFragment fragment) {
      42         BackHandledFragment second = fragment;
      43         FragmentManager fm = getSupportFragmentManager();
      44         FragmentTransaction ft = fm.beginTransaction();
      45         ft.replace(R.id.firstFragment, second, "other");
      46         ft.addToBackStack("tag");
      47         ft.commit();
      48     }
      49 
      50     @Override
      51     public void setSelectedFragment(BackHandledFragment selectedFragment) {
      52         this.mBackHandedFragment = selectedFragment;
      53     }
      54 
      55     @Override
      56     public void onBackPressed() {
      57         if (mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()) {
      58             if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
      59                 super.onBackPressed();
      60             } else {
      61                 if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
      62                     btnSecond.setVisibility(View.VISIBLE);
      63                 }
      64                 getSupportFragmentManager().popBackStack();
      65             }
      66         }
      67     }
      68 }

      5.分別添加兩個子級Fragment,FirstFragment.java和SecondFragment.java,代碼分別如下:

      FirstFragment.java

       1 package com.example.testdemo;
       2 
       3 import android.os.Bundle;
       4 import android.support.annotation.Nullable;
       5 import android.support.v4.app.FragmentManager;
       6 import android.support.v4.app.FragmentTransaction;
       7 import android.view.LayoutInflater;
       8 import android.view.View;
       9 import android.view.View.OnClickListener;
      10 import android.view.ViewGroup;
      11 import android.widget.Button;
      12 
      13 public class FirstFragment extends BackHandledFragment {
      14     private View myView;
      15     private Button btnSecond;
      16 
      17     @Override
      18     public View onCreateView(LayoutInflater inflater,
      19             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      20         myView = inflater.inflate(R.layout.fragment_first, null);
      21         initView();
      22         return myView;
      23     }
      24 
      25     private void initView() {
      26         btnSecond = (Button) myView.findViewById(R.id.btnSecond);
      27         btnSecond.setOnClickListener(new OnClickListener() {
      28 
      29             @Override
      30             public void onClick(View v) {
      31                 SecondFragment second = new SecondFragment();
      32                 FragmentManager fm = getFragmentManager();
      33                 FragmentTransaction ft = fm.beginTransaction();
      34                 ft.replace(R.id.firstFragment, second);
      35                 ft.addToBackStack("tag");
      36                 ft.commit();
      37             }
      38         });
      39     }
      40 
      41     @Override
      42     protected boolean onBackPressed() {
      43         return false;
      44     }
      45 
      46 }

      SecondFragment.java

       1 package com.example.testdemo;
       2 
       3 import android.os.Bundle;
       4 import android.support.annotation.Nullable;
       5 import android.view.LayoutInflater;
       6 import android.view.View;
       7 import android.view.ViewGroup;
       8 
       9 public class SecondFragment extends BackHandledFragment {
      10 
      11     private View mView;
      12 
      13     @Override
      14     public View onCreateView(LayoutInflater inflater,
      15             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      16         mView = inflater.inflate(R.layout.fragment_second, null);
      17         return mView;
      18     }
      19 
      20     @Override
      21     protected boolean onBackPressed() {
      22         return false;
      23     }
      24 
      25 }

      6.三個布局文件代碼如下:
      activity_main.xml

       1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       2     xmlns:tools="http://schemas.android.com/tools"
       3     android:layout_width="match_parent"
       4     android:layout_height="match_parent"
       5     android:orientation="vertical" >
       6 
       7     <TextView
       8         android:layout_width="wrap_content"
       9         android:layout_height="wrap_content"
      10         android:layout_centerInParent="true"
      11         android:text="FragmentActivity 父界面"
      12         android:textSize="26sp" />
      13 
      14     <Button
      15         android:id="@+id/btnSecond"
      16         android:layout_width="wrap_content"
      17         android:layout_height="wrap_content"
      18         android:layout_alignParentBottom="true"
      19         android:text="跳轉到FirstFragment" />
      20 
      21     <FrameLayout
      22         android:id="@+id/firstFragment"
      23         android:layout_width="match_parent"
      24         android:layout_height="match_parent" >
      25     </FrameLayout>
      26 
      27 </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="打開SecondFragment" />
      
      </RelativeLayout>

       

      fragment_second.xml

       1 <?xml version="1.0" encoding="utf-8"?>
       2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       3     android:layout_width="match_parent"
       4     android:layout_height="match_parent"
       5     android:background="#e5e5e5"
       6     android:orientation="vertical" >
       7 
       8     <TextView
       9         android:layout_width="wrap_content"
      10         android:layout_height="wrap_content"
      11         android:layout_centerInParent="true"
      12         android:text="SecondFragment"
      13         android:textColor="#000000"
      14         android:textSize="26sp" />
      15 
      16 </RelativeLayout>

       

      7.最后奉上實例鏈接:

      https://files.cnblogs.com/_ymw/TestDemo

      posted @ 2015-01-16 10:19  _YMW  閱讀(27737)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 日韩人妻无码精品专区综合网| 91老肥熟女九色老女人| 摸丰满大乳奶水www免费| 精品人妻伦一二三区久久| 安仁县| 亚洲第三十四九中文字幕| 精品国产高清中文字幕| 久久综合久中文字幕青草| 色噜噜在线视频免费观看| 中文熟妇人妻av在线| 久久精品蜜芽亚洲国产AV| 一区二区三区无码视频免费福利| 精品国产91久久粉嫩懂色| 日韩有码精品中文字幕| 日韩精品区一区二区三vr| 狠狠躁夜夜躁人人爽天天古典| 国产精品一区二区三区日韩| 亚洲熟妇熟女久久精品综合 | 成人区人妻精品一区二区| 国内精品免费久久久久电影院97| 国产91麻豆视频免费看| 国产精品熟女孕妇一区二区| 精品国产成人国产在线观看| 国内揄拍国内精品对久久| 国产精品无码无卡在线观看久| 亚洲欧美综合中文| 国产天美传媒性色av高清| 色狠狠色婷婷丁香五月| 欧美精品一区二区三区中文字幕 | 疯狂做受XXXX高潮国产| 最新亚洲av日韩av二区| 91老肥熟女九色老女人| 久久精品夜夜夜夜夜久久| 亚洲午夜无码久久久久蜜臀av| 97久久久精品综合88久久| 色综合中文综合网| 国产啪视频免费观看视频| 亚洲国产日韩欧美一区二区三区| 久久日韩精品一区二区五区| 亚洲欧洲日韩国内精品| 亚洲夂夂婷婷色拍ww47|