Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Square Fragmentやめるってよ

ninjinkun
October 22, 2014

Square Fragmentやめるってよ

『【翻訳】Android Fragmentへの反対声明』の解説
http://ninjinkun.hatenablog.com/entry/2014/10/16/234611

ninjinkun

October 22, 2014
Tweet

More Decks by ninjinkun

Other Decks in Programming

Transcript

  1. 'SBHNFOUΛੜ੒͢Δຐ๏ w 'SBHNFOU͸ϦϑϨΫγϣϯΛ࢖ͬͯ࠶ੜ੒͞ΕΔ w ಗ໊ΫϥεͰ'SBHNFOUΛ࡞Δͱɺ0VUFS$MBTTͷ ࢀর͕ͳͯ͘࠶ੜ੒࣌ʹࢮ͵ w +BWBͷϥΠϑαΠΫϧͱ'SBHNFOUͷϥΠϑαΠΫ ϧ͕ϛεϚον DialogFragment

    dialogFragment = new DialogFragment() { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ... } }; dialogFragment.show(fragmentManager, tag);
  2. public class MainFragment extends Fragment { public interface OnOkBtnClickListener {

    public void onOkClicked(); } private OnOkBtnClickListener mListener; public void setOnOkBtnClickListener(OnOkBtnClickListener l) { mListener = l; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button okBtn = new Button(inflater.getContext()); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mListener != null) { mListener.onOkClicked(); } } }); return okBtn; } } ࠶ੜ੒࣌ʹ-JTUFOFS͕ফ͑Δ IUUQZBO[NCMPHTQPUKQGSBHNFOUBDUJWJUZIUNM
  3. public class MainFragment2 extends Fragment { public interface OnOkBtnClickListener {

    public void onOkClicked(); } private OnOkBtnClickListener mListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof OnOkBtnClickListener == false) { throw new ClassCastException("activity ͕ OnOkBtnClickListener Λ࣮૷͍ͯ͠·ͤΜ."); } mListener = ((OnOkBtnClickListener) activity); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button okBtn = new Button(inflater.getContext()); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.onOkClicked(); } } }); return okBtn; } }
  4. public class DualPaneContainer extends LinearLayout implements Container { private MyDetailView

    detailView; ! public DualPaneContainer(Context context, AttributeSet attrs) { super(context, attrs); } ! @Override protected void onFinishInflate() { super.onFinishInflate(); detailView = (MyDetailView) getChildAt(1); } ! public boolean onBackPressed() { return false; } ! @Override public void showItem(String item) { detailView.setItem(item); } }
  5. public class MainActivity extends Activity { private Container container; !

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); container = (Container) findViewById(R.id.container); } ! public Container getContainer() { return container; } ! @Override public void onBackPressed() { boolean handled = container.onBackPressed(); if (!handled) { finish(); } } }
  6. public class MyDetailView extends LinearLayout { TextView textView; DetailPresenter presenter;

    ! public MyDetailView(Context context, AttributeSet attrs) { super(context, attrs); presenter = new DetailPresenter(); } ! @Override protected void onFinishInflate() { super.onFinishInflate(); presenter.setView(this); textView = (TextView) findViewById(R.id.text); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { presenter.buttonClicked(); } }); } ! public void setItem(String item) { textView.setText(item); } }
  7. class EditDiscountPresenter { // ... public void saveDiscount() { EditDiscountView

    view = getView(); String name = view.getName(); if (isBlank(name)) { view.showNameRequiredWarning(); return; } if (isNewDiscount()) { createNewDiscountAsync(name, view.getAmount(), view.isPercentage()); } else { updateNewDiscountAsync(discountId, name, view.getAmount(), view.isPercentage()); } close(); } }