Fragments

This commit is contained in:
Sergey Solovyev 2012-09-26 21:10:18 +04:00
parent ff1dd19018
commit 5371dbaac1
7 changed files with 1037 additions and 953 deletions

View File

@ -15,10 +15,62 @@
a:background="@color/default_background" a:background="@color/default_background"
a:baselineAligned="false"> a:baselineAligned="false">
<include layout="@layout/main_first_pane" <LinearLayout
a:layout_height="match_parent" a:layout_height="match_parent"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_weight="1"/> a:layout_weight="1"
a:orientation="vertical"
a:layout_gravity="center">
<LinearLayout a:id="@+id/editorContainer"
a:layout_weight="4"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_left_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<include layout="@layout/calc_erase_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<LinearLayout a:id="@+id/displayContainer"
a:layout_margin="@dimen/display_margin"
a:layout_weight="4"
a:layout_width="0dp"
a:layout_height="match_parent"/>
<include layout="@layout/calc_clear_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<include layout="@layout/calc_right_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
</LinearLayout>
<LinearLayout a:id="@+id/keyboardContainer"
a:layout_weight="3"
a:layout_width="match_parent"
a:layout_height="0dp"/>
</LinearLayout>
<LinearLayout a:id="@+id/main_second_pane" <LinearLayout a:id="@+id/main_second_pane"

View File

@ -38,7 +38,7 @@ import org.solovyev.common.text.StringUtils;
public class CalculatorActivity extends SherlockFragmentActivity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { public class CalculatorActivity extends SherlockFragmentActivity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull @NotNull
public static final String TAG = "Calculator++"; public static final String TAG = CalculatorActivity.class.getSimpleName();
private static final int HVGA_WIDTH_PIXELS = 320; private static final int HVGA_WIDTH_PIXELS = 320;
@ -64,10 +64,12 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(layout.getLayoutId()); activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(layout.getLayoutId(), TAG);
activityHelper.logDebug("onCreate");
activityHelper.onCreate(this, savedInstanceState); activityHelper.onCreate(this, savedInstanceState);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.logDebug("super.onCreate");
if (findViewById(R.id.main_second_pane) != null) { if (findViewById(R.id.main_second_pane) != null) {
activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_second_pane); activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_second_pane);
@ -201,29 +203,6 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
getCalculator().evaluate(); getCalculator().evaluate();
} }
/*
**********************************************************************
*
* MENU
*
**********************************************************************
*/
/* @Override
public boolean onPrepareOptionsMenu(Menu menu) {
return this.menu.onPrepareOptionsMenu(this, menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return this.menu.onCreateOptionsMenu(this, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return menu.onOptionsItemSelected(this, item);
}*/
/** /**
* The font sizes in the layout files are specified for a HVGA display. * The font sizes in the layout files are specified for a HVGA display.
@ -239,7 +218,14 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
} }
@Override @Override
protected void onPause() {
super.onPause();
activityHelper.onPause(this);
}
@Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -258,6 +244,8 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
BillingController.unregisterObserver(billingObserver); BillingController.unregisterObserver(billingObserver);
} }
activityHelper.onDestroy(this);
super.onDestroy(); super.onDestroy();
} }

View File

@ -3,7 +3,6 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -29,6 +28,8 @@ public interface CalculatorActivityHelper {
void onResume(@NotNull SherlockFragmentActivity activity); void onResume(@NotNull SherlockFragmentActivity activity);
void onResume(@NotNull Activity activity); void onResume(@NotNull Activity activity);
void onDestroy(@NotNull SherlockFragmentActivity activity);
void addTab(@NotNull SherlockFragmentActivity activity, void addTab(@NotNull SherlockFragmentActivity activity,
@NotNull String tag, @NotNull String tag,
@NotNull Class<? extends Fragment> fragmentClass, @NotNull Class<? extends Fragment> fragmentClass,
@ -36,4 +37,8 @@ public interface CalculatorActivityHelper {
int captionResId, int parentViewId); int captionResId, int parentViewId);
void restoreSavedTab(@NotNull SherlockFragmentActivity activity); void restoreSavedTab(@NotNull SherlockFragmentActivity activity);
void logDebug(@NotNull String message);
void onPause(@NotNull SherlockFragmentActivity activity);
} }

View File

@ -5,12 +5,12 @@ import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log;
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener;
import java.util.ArrayList; import java.util.ArrayList;
@ -51,8 +51,12 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
private int navPosition = 0; private int navPosition = 0;
public CalculatorActivityHelperImpl(int layoutId) { @NotNull
private String logTag = "CalculatorActivity";
public CalculatorActivityHelperImpl(int layoutId, @NotNull String logTag) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.logTag = logTag;
} }
public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) { public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) {
@ -62,6 +66,8 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
@Override @Override
public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) { public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) {
Log.d(logTag + ": helper", "onCreate");
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
this.theme = CalculatorPreferences.Gui.getTheme(preferences); this.theme = CalculatorPreferences.Gui.getTheme(preferences);
@ -94,9 +100,12 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
} }
@Override @Override
public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) { public void logDebug(@NotNull String message) {
FragmentUtils.detachFragments(activity, fragmentTags); Log.d(logTag, message);
}
@Override
public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) {
onSaveInstanceState((Activity) activity, outState); onSaveInstanceState((Activity) activity, outState);
outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex()); outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex());
} }
@ -115,6 +124,14 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
} }
} }
@Override
public void onDestroy(@NotNull SherlockFragmentActivity activity) {
}
@Override
public void onPause(@NotNull SherlockFragmentActivity activity) {
}
@Override @Override
public void addTab(@NotNull SherlockFragmentActivity activity, public void addTab(@NotNull SherlockFragmentActivity activity,
@NotNull String tag, @NotNull String tag,

View File

@ -111,7 +111,7 @@ public class CalculatorApplication extends android.app.Application {
} }
@NotNull @NotNull
public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId) { public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId, @NotNull String logTag) {
return new CalculatorActivityHelperImpl(layoutResId); return new CalculatorActivityHelperImpl(layoutResId, logTag);
} }
} }

View File

@ -8,6 +8,7 @@ package org.solovyev.android.calculator.history;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -42,6 +43,8 @@ import java.util.List;
*/ */
public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener {
@NotNull
private static final String TAG = "CalculatorHistoryFragment";
public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() { public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() {
@Override @Override
@ -68,17 +71,27 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
logDebug("onCreate");
}
private int logDebug(@NotNull String msg) {
return Log.d(TAG + ": " + getTag(), msg);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.history_fragment, container, false); final View result = inflater.inflate(R.layout.history_fragment, container, false);
logDebug("onCreateView");
return result;
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
logDebug("onViewCreated");
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>()); adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
setListAdapter(adapter); setListAdapter(adapter);
@ -132,7 +145,9 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Override @Override
public void onDestroy() { public void onDestroy() {
if ( this.adView != null ) { logDebug("onDestroy");
if ( this.adView != null ) {
this.adView.destroy(); this.adView.destroy();
} }
super.onDestroy(); super.onDestroy();
@ -142,7 +157,10 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); logDebug("onResume");
super.onResume();
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
@ -151,6 +169,8 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Override @Override
public void onPause() { public void onPause() {
logDebug("onPause");
super.onPause(); super.onPause();
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
@ -274,9 +294,11 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
if ( calculatorEventType == CalculatorEventType.history_state_added ) { if ( calculatorEventType == CalculatorEventType.history_state_added ) {
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
logDebug("onCalculatorEvent");
updateAdapter(); updateAdapter();
} }
}); });

View File

@ -20,7 +20,7 @@ import org.solovyev.android.calculator.*;
public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull @NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.main_empty); private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName());
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {