標題:
隨堂測驗0427
[打印本頁]
作者:
顏子翔
時間:
2019-4-27 11:05
標題:
隨堂測驗0427
本日已複習104
請各位將測驗完畢的解答貼上
作者:
黃璽安
時間:
2019-4-27 11:05
package com.tqc.gdd01;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class GDD01 extends Activity
{
public static boolean bIfDebug = false;
public static String TAG = "HIPPO_DEBUG";
boolean detailPage = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(savedInstanceState == null)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ListFragment listFragment = new ListFragment();
ft.add(R.id.displayList, listFragment, "List_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
if(findViewById(R.id.displayDetail) != null)
{
// Landscape
detailPage = true;
getFragmentManager().popBackStack();
// 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
if(detailFragment == null)
{
// TO DO
detailFragment = new DetailFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.displayDetail, detailFragment, "Detail_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
public void onButtonClicked(String strText)
{
// 依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
if(detailPage)
{
// Landscape
//TO DO
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
detailFragment.updateDavidContent(strText);
}
else
{
//TO DO
DetailFragment detailFragment = new DetailFragment();
detailFragment.updateDavidContent(strText);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.displayList, detailFragment, null);
ft.commit();
}
}
}
複製代碼
作者:
李知易
時間:
2019-4-27 11:06
package com.tqc.gdd01;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class GDD01 extends Activity
{
public static boolean bIfDebug = false;
public static String TAG = "HIPPO_DEBUG";
boolean detailPage = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(savedInstanceState == null)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ListFragment listFragment = new ListFragment();
ft.add(R.id.displayList, listFragment, "List_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
if(findViewById(R.id.displayDetail) != null)
{
// Landscape
detailPage = true;
getFragmentManager().popBackStack();
// 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
if(detailFragment == null)
{
// TO DO
detailFragment = new DetailFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.displayDetail,detailFragment,null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
public void onButtonClicked(String strText)
{
// 依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
if(detailPage)
{
// Landscape
//TO DO
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
detailFragment.updateDavidContent(strText);
}
else
{
//TO DO
DetailFragment detailFragment = new DetailFragment();
detailFragment.updateDavidContent(strText);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.displayList, detailFragment, null);
ft.commit();
}
}
}
複製代碼
作者:
張健勳
時間:
2019-4-27 11:07
package com.tqc.gdd01;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class GDD01 extends Activity {
public static boolean bIfDebug = false;
public static String TAG = "HIPPO_DEBUG";
boolean detailPage = false; //detailPage 是否配置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) { //初次建立 listFragment 元件
FragmentTransaction ft = getFragmentManager().beginTransaction();
ListFragment listFragment = new ListFragment();
ft.add(R.id.displayList, listFragment, "List_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //設定轉場動畫
ft.commit(); //交託
}
//如果 displaydetail 這個元件存在,代表此時為橫向擺放 (橫向擺放時會自動抓取資料夾 layout-land 下的 main.xml)
if (findViewById(R.id.displayDetail) != null) {
// Landscape
detailPage = true;
getFragmentManager().popBackStack();
// 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
if (detailFragment == null) { //初次建立 detailFragment 元件
// TO DO
detailFragment = new DetailFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.displayDetail, detailFragment, null);
ft.commit();
}
}
}
public void onButtonClicked(String strText) {
// 依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
if (detailPage) {
// Landscape
//TO DO
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
detailFragment.updateDavidContent(strText);
} else {
//TO DO
DetailFragment detailFragment=new DetailFragment();
detailFragment.updateDavidContent(strText);
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.addToBackStack(null); //加到後方堆疊區,使按 "返回鍵" 時不會整個畫面小
ft.replace(R.id.displayList, detailFragment, null);
//replace() 的功能等同於先呼叫 remove() 移除目前的 Fragment,再呼叫 add() 加入另一個 Fragment
ft.commit();
}
}
}
複製代碼
作者:
陳泓瑜
時間:
2019-4-27 11:08
package com.tqc.gdd01;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class GDD01 extends Activity
{
public static boolean bIfDebug = false;
public static String TAG = "HIPPO_DEBUG";
boolean detailPage = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(savedInstanceState == null)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ListFragment listFragment = new ListFragment();
ft.add(R.id.displayList, listFragment, "List_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
if(findViewById(R.id.displayDetail) != null)
{
// Landscape
detailPage = true;
getFragmentManager().popBackStack();
// 主程式setContentView()之後,判斷橫式UI中ID為displayDetail的FrameLayout是否存在,置換不同的Fragment於其中
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
if(detailFragment == null)
{
// TO DO
FragmentTransaction ft = getFragmentManager().beginTransaction();
detailFragment = new DetailFragment();
ft.add(R.id.displayDetail, detailFragment, "Detail_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
public void onButtonClicked(String strText)
{
// 依據直式或橫式按鈕事件也不同,請為直式切換頁面Fragment,而橫式為置換右方FrameLayout。
if(detailPage)
{
// Landscape
//TO DO
DetailFragment detailFragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.displayDetail);
detailFragment.updateDavidContent(strText);
}
else
{
//TO DO
DetailFragment detailFragment = new DetailFragment();
detailFragment.updateDavidContent(strText);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.displayList, detailFragment, "Detail_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
複製代碼
package com.tqc.gdd01;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Hippo on 2015/8/17.
*/
public class ListFragment extends Fragment {
public static boolean bIfDebug = false;
public static String TAG = "HIPPO_DEBUG";
public static GDD01 activity;
private Button mButton01, mButton02;
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//呼叫 getActivity() 進行初始化
activity=(GDD01)getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
rootView = inflater.inflate(R.layout.list_view, container, false);
ViewGroup p = (ViewGroup) rootView.getParent();
if (p != null) {
p.removeAllViewsInLayout();
}
return rootView;
//return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mButton01 = (Button) rootView.findViewById(R.id.list_view_button1);
mButton02 = (Button) rootView.findViewById(R.id.list_view_button2);
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
ListFragment.this.activity.onButtonClicked(getString(R.string.detail_body1));
}
});
mButton02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
ListFragment.this.activity.onButtonClicked(getString(R.string.detail_body2));
}
});
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2