總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。
顯示具有 Fragment 標籤的文章。 顯示所有文章
顯示具有 Fragment 標籤的文章。 顯示所有文章

2015年3月1日 星期日

Fragment 呼叫 Fragment

Bundle arguments = new Bundle();
int menuIndex = 1;
arguments.putInt("expand_menu_type", menuIndex);
BFragment fragment = new BFragment();
fragment.setArguments(arguments);
getFragmentManager().beginTransaction().replace(R.id.expand_menu, fragment, BFragment.class.getName()).addToBackStack(BFragment.class.getName()).commitAllowingStateLoss();

//點擊widget id R.id.expand_menu
//欲前往Fragment BFragment
//Class Name為BFragment.class.getName()
//

2015年2月9日 星期一

implements OnItemClickListener 的class 取得activity

class xxx extends Fragment implements OnItemClickListener

【情境1】切換activity
Intent intent = new Intent(this, ooo.class);←false
Intent intent = new Intent(getActivity(), ooo.class);←true

【情境2】show dialog
↓false
new AlertDialog.Builder(this).setTitle("")
   .setMessage(R.string.nonetworkalert)
    .setCancelable(false)
   .setPositiveButton(R.string.sure,
      new DialogInterface.OnClickListener()
      {
          public void onClick(DialogInterface dialoginterface, int i)
          {
          }
      }
   ).show();

↓true
new AlertDialog.Builder(getActivity()).setTitle("")
   .setMessage(R.string.nonetworkalert)
    .setCancelable(false)
   .setPositiveButton(R.string.sure,
      new DialogInterface.OnClickListener()
      {
          public void onClick(DialogInterface dialoginterface, int i)
          {
          }
      }
   ).show();

2014年4月9日 星期三

【Fragment, IntentService】在Thread中執行toast

//先宣告
Activity activity;

//當此Fragment被呼叫起來時會執行
@Override
    public void onAttach(Activity activity) {
        if (Log.IS_DEBUG) Log.i(TAG, TAG + " onAttach");
        super.onAttach(activity);
        this.activity = activity;
    }


new Thread() {
    public void run() {

        //在Thread中執行toast
        showToastByRunnable(activity, activity.getResources().getString("invalidCharacter"), Toast.LENGTH_LONG);
    }
}

//自訂showToastByRunnable方法
private void showToastByRunnable(final Context context, final CharSequence text, final int duration)     { 
        Handler handler = new Handler(Looper.getMainLooper()); 
        handler.post(new Runnable() { 
            @Override 
            public void run() { 
                Toast.makeText(context, text, duration).show(); 
            } 
        }); 
    }

2014年4月6日 星期日

【Fragment】 用Bundle傳遞資料


Fragment是Android 3.0後提出的一種東西

現在有FragmentA與FragmentB這兩個Fragment

FragmentA要透過Bundle傳遞資料給FragmentB〉
FragmentB fragmentB = new FragmentB();
Bundle bundle = new Bundle();
String exception = "yummy";

// "EXCEPTION"就像暗號, 由此來判別儲存的值
bundle.putString("EXCEPTION", exception);        
fragmentB.setArguments(bundle);



FragmentB要知道FragmentA傳來什麼資料〉String whatException = getArguments("EXCEPTION");