返回列表 發帖

隨堂測驗0427

本日已複習104

請各位將測驗完畢的解答貼上

  1. package com.tqc.gdd01;

  2. import android.app.Activity;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;

  5. public class GDD01 extends Activity
  6. {
  7.   public static boolean bIfDebug = false;
  8.   public static String TAG = "HIPPO_DEBUG";
  9.   boolean detailPage = false;

  10.   @Override
  11.   protected void onCreate(Bundle savedInstanceState)
  12.   {
  13.     super.onCreate(savedInstanceState);
  14.     setContentView(R.layout.main);

  15.     if(savedInstanceState == null)
  16.     {
  17.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  18.       ListFragment listFragment = new ListFragment();
  19.       ft.add(R.id.displayList, listFragment, "List_Fragment");
  20.       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  21.       ft.commit();
  22.     }

  23.     if(findViewById(R.id.displayDetail) != null)
  24.     {
  25.       // Landscape
  26.       detailPage = true;
  27.       getFragmentManager().popBackStack();

  28.       // 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
  29.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  30.       if(detailFragment == null)
  31.       {
  32.         // TO DO
  33.         detailFragment = new DetailFragment();
  34.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  35.         ft.add(R.id.displayDetail, detailFragment, "Detail_Fragment");
  36.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  37.         ft.commit();
  38.       }
  39.     }
  40.   }

  41.   public void onButtonClicked(String strText)
  42.   {
  43.     //  依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
  44.     if(detailPage)
  45.     {
  46.       // Landscape
  47.       //TO DO
  48.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  49.       detailFragment.updateDavidContent(strText);
  50.     }
  51.     else
  52.     {
  53.       //TO DO
  54.       DetailFragment detailFragment = new DetailFragment();
  55.       detailFragment.updateDavidContent(strText);
  56.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  57.       ft.addToBackStack(null);
  58.       ft.replace(R.id.displayList, detailFragment, null);
  59.       ft.commit();
  60.     }
  61.   }
  62. }
複製代碼

TOP

  1. package com.tqc.gdd01;

  2. import android.app.Activity;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;

  5. public class GDD01 extends Activity
  6. {
  7.   public static boolean bIfDebug = false;
  8.   public static String TAG = "HIPPO_DEBUG";
  9.   boolean detailPage = false;

  10.   @Override
  11.   protected void onCreate(Bundle savedInstanceState)
  12.   {
  13.     super.onCreate(savedInstanceState);
  14.     setContentView(R.layout.main);

  15.     if(savedInstanceState == null)
  16.     {
  17.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  18.       ListFragment listFragment = new ListFragment();
  19.       ft.add(R.id.displayList, listFragment, "List_Fragment");
  20.       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  21.       ft.commit();
  22.     }

  23.     if(findViewById(R.id.displayDetail) != null)
  24.     {
  25.       // Landscape
  26.       detailPage = true;
  27.       getFragmentManager().popBackStack();

  28.       // 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
  29.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  30.       if(detailFragment == null)
  31.       {
  32.         // TO DO
  33.         detailFragment = new DetailFragment();
  34.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  35.         ft.add(R.id.displayDetail,detailFragment,null);
  36.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  37.         ft.commit();
  38.       }
  39.     }
  40.   }

  41.   public void onButtonClicked(String strText)
  42.   {
  43.     //  依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
  44.     if(detailPage)
  45.     {
  46.       // Landscape
  47.       //TO DO
  48.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  49.       detailFragment.updateDavidContent(strText);
  50.     }
  51.     else
  52.     {
  53.       //TO DO
  54.       DetailFragment detailFragment = new DetailFragment();
  55.       detailFragment.updateDavidContent(strText);
  56.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  57.       ft.addToBackStack(null);
  58.       ft.replace(R.id.displayList, detailFragment, null);
  59.       ft.commit();

  60.     }
  61.   }
  62. }
複製代碼

TOP

  1. package com.tqc.gdd01;

  2. import android.app.Activity;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;

  5. public class GDD01 extends Activity {
  6.   public static boolean bIfDebug = false;
  7.   public static String TAG = "HIPPO_DEBUG";
  8.   boolean detailPage = false;   //detailPage 是否配置

  9.   @Override
  10.   protected void onCreate(Bundle savedInstanceState) {
  11.     super.onCreate(savedInstanceState);
  12.     setContentView(R.layout.main);

  13.     if (savedInstanceState == null) {   //初次建立 listFragment 元件
  14.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  15.       ListFragment listFragment = new ListFragment();
  16.       ft.add(R.id.displayList, listFragment, "List_Fragment");
  17.       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);  //設定轉場動畫
  18.       ft.commit();   //交託
  19.     }

  20.     //如果 displaydetail 這個元件存在,代表此時為橫向擺放 (橫向擺放時會自動抓取資料夾 layout-land 下的 main.xml)
  21.     if (findViewById(R.id.displayDetail) != null) {
  22.       // Landscape
  23.       detailPage = true;
  24.       getFragmentManager().popBackStack();

  25.       // 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
  26.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  27.       if (detailFragment == null) {   //初次建立 detailFragment 元件
  28.         // TO DO
  29.         detailFragment = new DetailFragment();
  30.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  31.         ft.add(R.id.displayDetail, detailFragment, null);
  32.         ft.commit();
  33.       }
  34.     }
  35.   }

  36.   public void onButtonClicked(String strText) {
  37.     //  依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
  38.     if (detailPage) {
  39.       // Landscape
  40.       //TO DO
  41.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  42.       detailFragment.updateDavidContent(strText);
  43.     } else {
  44.       //TO DO
  45.       DetailFragment detailFragment=new DetailFragment();
  46.       detailFragment.updateDavidContent(strText);
  47.       FragmentTransaction ft=getFragmentManager().beginTransaction();
  48.       ft.addToBackStack(null);  //加到後方堆疊區,使按 "返回鍵" 時不會整個畫面小
  49.       ft.replace(R.id.displayList, detailFragment, null);
  50.       //replace() 的功能等同於先呼叫 remove() 移除目前的 Fragment,再呼叫 add() 加入另一個 Fragment
  51.       ft.commit();
  52.     }
  53.   }
  54. }
複製代碼

TOP

  1. package com.tqc.gdd01;

  2. import android.app.Activity;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;

  5. public class GDD01 extends Activity
  6. {
  7.   public static boolean bIfDebug = false;
  8.   public static String TAG = "HIPPO_DEBUG";
  9.   boolean detailPage = false;

  10.   @Override
  11.   protected void onCreate(Bundle savedInstanceState)
  12.   {
  13.     super.onCreate(savedInstanceState);
  14.     setContentView(R.layout.main);

  15.     if(savedInstanceState == null)
  16.     {
  17.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  18.       ListFragment listFragment = new ListFragment();
  19.       ft.add(R.id.displayList, listFragment, "List_Fragment");
  20.       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  21.       ft.commit();
  22.     }

  23.     if(findViewById(R.id.displayDetail) != null)
  24.     {
  25.       // Landscape
  26.       detailPage = true;
  27.       getFragmentManager().popBackStack();

  28.       // 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
  29.       DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
  30.       if(detailFragment == null)
  31.       {
  32.         // TO DO
  33.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  34.         detailFragment = new DetailFragment();
  35.         ft.add(R.id.displayDetail, detailFragment, "Detail_Fragment");
  36.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  37.         ft.commit();
  38.       }
  39.     }
  40.   }

  41.   public void onButtonClicked(String strText)
  42.   {
  43.     //  依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
  44.     if(detailPage)
  45.     {
  46.       // Landscape
  47.       //TO DO
  48.       DetailFragment detailFragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.displayDetail);
  49.       detailFragment.updateDavidContent(strText);
  50.     }
  51.     else
  52.     {
  53.       //TO DO
  54.       DetailFragment detailFragment = new DetailFragment();
  55.       detailFragment.updateDavidContent(strText);
  56.       FragmentTransaction ft = getFragmentManager().beginTransaction();
  57.       ft.addToBackStack(null);
  58.       ft.replace(R.id.displayList, detailFragment, "Detail_Fragment");
  59.       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  60.       ft.commit();
  61.     }
  62.   }
  63. }
複製代碼
  1. package com.tqc.gdd01;

  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.Button;

  8. /**
  9. * Created by Hippo on 2015/8/17.
  10. */
  11. public class ListFragment extends Fragment {
  12.   public static boolean bIfDebug = false;
  13.   public static String TAG = "HIPPO_DEBUG";
  14.   public static GDD01 activity;
  15.   private Button mButton01, mButton02;
  16.   private View rootView;

  17.   @Override
  18.   public void onCreate(Bundle savedInstanceState) {
  19.     super.onCreate(savedInstanceState);
  20.     //呼叫 getActivity() 進行初始化
  21.     activity=(GDD01)getActivity();
  22.   }

  23.   @Override
  24.   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  25.     if (container == null) {
  26.       return null;
  27.     }
  28.     rootView = inflater.inflate(R.layout.list_view, container, false);
  29.     ViewGroup p = (ViewGroup) rootView.getParent();
  30.     if (p != null) {
  31.       p.removeAllViewsInLayout();
  32.     }
  33.     return rootView;
  34.     //return super.onCreateView(inflater, container, savedInstanceState);
  35.   }

  36.   @Override
  37.   public void onActivityCreated(Bundle savedInstanceState) {
  38.     super.onActivityCreated(savedInstanceState);
  39.     mButton01 = (Button) rootView.findViewById(R.id.list_view_button1);
  40.     mButton02 = (Button) rootView.findViewById(R.id.list_view_button2);
  41.     mButton01.setOnClickListener(new Button.OnClickListener() {
  42.       @Override
  43.       public void onClick(View v) {
  44.         ListFragment.this.activity.onButtonClicked(getString(R.string.detail_body1));
  45.       }
  46.     });
  47.     mButton02.setOnClickListener(new Button.OnClickListener() {
  48.       @Override
  49.       public void onClick(View v) {
  50.         ListFragment.this.activity.onButtonClicked(getString(R.string.detail_body2));
  51.       }
  52.     });
  53.   }
  54. }
複製代碼
https://www.facebook.com/DABRiXPERT6584

TOP

返回列表