Fragments

This commit is contained in:
serso 2012-09-25 17:30:44 +04:00
parent c1e6389024
commit 009f536772
7 changed files with 334 additions and 289 deletions

View File

@ -20,8 +20,6 @@
</activity> </activity>
<service android:name=".CalculationServiceImpl"/>
<!--NOTE: a:configChanges="orientation|keyboardHidden" is needed to correct work of dialog windows (not to close them on orientation change) --> <!--NOTE: a:configChanges="orientation|keyboardHidden" is needed to correct work of dialog windows (not to close them on orientation change) -->
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_settings" android:name=".CalculatorPreferencesActivity"/> <activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_settings" android:name=".CalculatorPreferencesActivity"/>

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_vars_button"/>
<include layout="@layout/calc_copy_button"/>
<include layout="@layout/calc_seven_digit_button"/>
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_multiplication_button"/>
<include layout="@layout/calc_division_button"/>
<include layout="@layout/calc_equals_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_functions_button"/>
<include layout="@layout/calc_paste_button"/>
<include layout="@layout/calc_four_digit_button"/>
<include layout="@layout/calc_five_digit_button"/>
<include layout="@layout/calc_six_digit_button"/>
<include layout="@layout/calc_plus_button"/>
<include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_round_brackets_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_operators_button"/>
<include layout="@layout/calc_donate_button"/>
<include layout="@layout/calc_one_digit_button"/>
<include layout="@layout/calc_two_digit_button"/>
<include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_history_button"/>
</LinearLayout>
</LinearLayout>

View File

@ -8,16 +8,21 @@
<LinearLayout <LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android" xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_width="fill_parent" a:layout_width="match_parent"
a:id="@+id/ad_parent_view" a:layout_height="match_parent"
a:layout_height="fill_parent" a:id="@+id/main_layout"
a:orientation="vertical" a:orientation="vertical"
a:layout_gravity="center" a:layout_gravity="center"
a:background="#ff000000"> a:background="@color/default_background">
<include layout="@layout/calc_editor"/> <LinearLayout a:id="@+id/editorContainer"
a:layout_weight="2"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp"> <LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
@ -28,7 +33,7 @@
style="?controlButtonStyle" style="?controlButtonStyle"
a:onClick="moveLeftButtonClickHandler" a:onClick="moveLeftButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
@ -39,14 +44,13 @@
style="?controlImageButtonStyle" style="?controlImageButtonStyle"
a:onClick="eraseButtonClickHandler" a:onClick="eraseButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
<LinearLayout a:id="@+id/displayContainer"
<include layout="@layout/calc_display" a:layout_weight="4"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="match_parent"/>
a:layout_weight="4"/>
<org.solovyev.android.calculator.view.NumeralBasesButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.calculator.view.NumeralBasesButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
@ -59,7 +63,7 @@
style="?controlImageButtonStyle" style="?controlImageButtonStyle"
a:onClick="clearButtonClickHandler" a:onClick="clearButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
<org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
@ -71,48 +75,15 @@
style="?controlButtonStyle" style="?controlButtonStyle"
a:onClick="moveRightButtonClickHandler" a:onClick="moveRightButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
</LinearLayout> </LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp"> <LinearLayout a:id="@+id/keyboardContainer"
a:layout_weight="4"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<include layout="@layout/calc_vars_button"/>
<include layout="@layout/calc_copy_button"/>
<include layout="@layout/calc_seven_digit_button"/>
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_multiplication_button"/>
<include layout="@layout/calc_division_button"/>
<include layout="@layout/calc_equals_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_functions_button"/>
<include layout="@layout/calc_paste_button"/>
<include layout="@layout/calc_four_digit_button"/>
<include layout="@layout/calc_five_digit_button"/>
<include layout="@layout/calc_six_digit_button"/>
<include layout="@layout/calc_plus_button"/>
<include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_round_brackets_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_operators_button"/>
<include layout="@layout/calc_donate_button"/>
<include layout="@layout/calc_one_digit_button"/>
<include layout="@layout/calc_two_digit_button"/>
<include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_history_button"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -13,7 +13,7 @@
a:id="@+id/main_layout" a:id="@+id/main_layout"
a:orientation="vertical" a:orientation="vertical"
a:layout_gravity="center" a:layout_gravity="center"
a:background="#ff000000"> a:background="@color/default_background">
<LinearLayout a:id="@+id/editorContainer" <LinearLayout a:id="@+id/editorContainer"
a:layout_weight="2" a:layout_weight="2"

View File

@ -14,6 +14,7 @@ import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Log; import android.util.Log;
@ -80,17 +81,17 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorEditorFragment editorFragment = new CalculatorEditorFragment(); final CalculatorEditorFragment editorFragment = new CalculatorEditorFragment();
fragmentTransaction.add(R.id.editorContainer, editorFragment); fragmentTransaction.add(R.id.editorContainer, editorFragment, "editor");
fragmentTransaction.commit(); fragmentTransaction.commit();
fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorDisplayFragment displayFragment = new CalculatorDisplayFragment(); final CalculatorDisplayFragment displayFragment = new CalculatorDisplayFragment();
fragmentTransaction.add(R.id.displayContainer, displayFragment); fragmentTransaction.add(R.id.displayContainer, displayFragment, "display");
fragmentTransaction.commit(); fragmentTransaction.commit();
fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorKeyboardFragment keyboardFragment = new CalculatorKeyboardFragment(); final CalculatorKeyboardFragment keyboardFragment = new CalculatorKeyboardFragment();
fragmentTransaction.add(R.id.keyboardContainer, keyboardFragment); fragmentTransaction.add(R.id.keyboardContainer, keyboardFragment, "keyboard");
fragmentTransaction.commit(); fragmentTransaction.commit();
if (customTitleSupported) { if (customTitleSupported) {

View File

@ -90,9 +90,14 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
setOnDragListeners(root, dragPreferences, preferences); setOnDragListeners(root, dragPreferences, preferences);
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences); final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences);
((DragButton) root.findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener); final DragButton historyButton = getButton(root, R.id.historyButton);
if (historyButton != null) {
historyButton.setOnDragListener(historyOnDragListener);
}
((DragButton) root.findViewById(R.id.subtractionButton)).setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
if (subtractionButton != null) {
subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() {
@Override @Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) { if (dragDirection == DragDirection.down) {
@ -102,33 +107,42 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
return false; return false;
} }
}, dragPreferences), vibrator, preferences)); }, dragPreferences), vibrator, preferences));
}
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
((DragButton) root.findViewById(R.id.rightButton)).setOnDragListener(toPositionOnDragListener);
((DragButton) root.findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
final DragButton equalsButton = (DragButton) root.findViewById(R.id.equalsButton); final DragButton rightButton = getButton(root, R.id.rightButton);
if (rightButton != null) {
rightButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton leftButton = getButton(root, R.id.leftButton);
if (leftButton != null) {
leftButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton equalsButton = getButton(root, R.id.equalsButton);
if (equalsButton != null) { if (equalsButton != null) {
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
} }
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) root.findViewById(R.id.sixDigitButton); final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
if (angleUnitsButton != null) { if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences)); angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final NumeralBasesButton clearButton = (NumeralBasesButton) root.findViewById(R.id.clearButton); final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
if (clearButton != null) { if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences)); clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final DragButton varsButton = (DragButton) root.findViewById(R.id.varsButton); final DragButton varsButton = getButton(root, R.id.varsButton);
if (varsButton != null) { if (varsButton != null) {
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences)); varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final DragButton roundBracketsButton = (DragButton) root.findViewById(R.id.roundBracketsButton); final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton);
if (roundBracketsButton != null) { if (roundBracketsButton != null) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
} }
@ -162,6 +176,11 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
initMultiplicationButton(); initMultiplicationButton();
} }
@Nullable
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
return (T) root.findViewById(buttonId);
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -240,7 +259,10 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
} }
for (Integer dragButtonId : dragButtonIds) { for (Integer dragButtonId : dragButtonIds) {
((DragButton) root.findViewById(dragButtonId)).setOnDragListener(onDragListener); final DragButton button = getButton(root, dragButtonId);
if (button != null) {
button.setOnDragListener(onDragListener);
}
} }
} }
@ -273,7 +295,7 @@ public class CalculatorKeyboardFragment extends Fragment implements SharedPrefer
} }
private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) {
final View v = root.findViewById(id); final View v = getButton(root, id);
if (v instanceof DirectionDragButton ) { if (v instanceof DirectionDragButton ) {
final DirectionDragButton button = (DirectionDragButton)v; final DirectionDragButton button = (DirectionDragButton)v;
for (DragDirection dragDirection : dragDirections) { for (DragDirection dragDirection : dragDirections) {