@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { mPurchase = savedInstanceState.getParcelable(Purchase.PARCELABLE_NAME); } else if (getArguments() != null) { mPurchase = getArguments().getParcelable(Purchase.PARCELABLE_NAME); } else { mPurchase = new Purchase(); } setTitle(mPurchase.getName()); // Размещаем виджеты FragmentManager fragmentManager = getChildFragmentManager(); mWidgets = new ArrayList<Widget>(); Summary summary = new Summary(); mWidgets.add(summary); List list = new List(); mWidgets.add(list); fragmentManager .beginTransaction() .replace(R.id.summary_container, summary) .replace(R.id.list_container, list) .commit(); }
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mViewSwitcher = (ViewSwitcher) view.findViewById(R.id.switcher); mEditTitle = (EditText) view.findViewById(R.id.edit_title); App.setTypeface(mEditTitle, App.Type.ROBOTO_LIGHT); final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); // По нажатию на заголовок - редактирование mTitleView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mEditTitle.setText(mPurchase.getName()); mViewSwitcher.showNext(); mEditTitle.requestFocus(); imm.showSoftInput(mEditTitle, InputMethodManager.SHOW_FORCED); } }); // При потере фокуса - сброс mEditTitle.setOnFocusChangeListener( new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { mViewSwitcher.showNext(); } } }); // Нажатие Готово сохраняет изменения mEditTitle.setOnEditorActionListener( new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { String name = v.getText().toString(); if (name.length() != 0) { mPurchase.setName(name); mPurchase.save(); setTitle(name); updateTitle(); v.clearFocus(); return true; } } return false; } }); // Отмена по кнопке назад mEditTitle.setOnKeyListener( new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && mViewSwitcher.getCurrentView() == v) { v.clearFocus(); return true; } return false; } }); }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(Purchase.PARCELABLE_NAME, mPurchase); }