| 1 | package ro.ieval.unical; |
| 2 | |
| 3 | import android.app.Fragment; |
| 4 | import android.os.Bundle; |
| 5 | import android.view.LayoutInflater; |
| 6 | import android.view.View; |
| 7 | import android.view.ViewGroup; |
| 8 | import android.widget.AdapterView; |
| 9 | import android.widget.CompoundButton; |
| 10 | import android.widget.LinearLayout; |
| 11 | import android.widget.Spinner; |
| 12 | import android.widget.Switch; |
| 13 | |
| 14 | public final class TrivialFragment extends Fragment { |
| 15 | private static class OnItemSelectedListener implements android.widget.AdapterView.OnItemSelectedListener{ |
| 16 | private final View repeatCount; |
| 17 | private final View repeatUntil; |
| 18 | |
| 19 | public OnItemSelectedListener(final View repeatCount, final View repeatUntil) { |
| 20 | this.repeatCount=repeatCount; |
| 21 | this.repeatUntil=repeatUntil; |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public void onItemSelected(final AdapterView<?> whatever, final View ignored, final int pos, final long id) { |
| 26 | switch(pos){ |
| 27 | case 0: |
| 28 | repeatCount.setVisibility(View.GONE); |
| 29 | repeatUntil.setVisibility(View.GONE); |
| 30 | break; |
| 31 | case 1: |
| 32 | repeatCount.setVisibility(View.GONE); |
| 33 | repeatUntil.setVisibility(View.VISIBLE); |
| 34 | break; |
| 35 | case 2: |
| 36 | repeatCount.setVisibility(View.VISIBLE); |
| 37 | repeatUntil.setVisibility(View.GONE); |
| 38 | break; |
| 39 | default: |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | @Override public void onNothingSelected(final AdapterView<?> arg0) { /* do nothing */ } |
| 44 | } |
| 45 | |
| 46 | private static class OnCheckedChangeListener implements android.widget.CompoundButton.OnCheckedChangeListener{ |
| 47 | private final LinearLayout layout; |
| 48 | private final View root; |
| 49 | public OnCheckedChangeListener(final LinearLayout layout, final View root){ |
| 50 | this.layout=layout; |
| 51 | this.root=root; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { |
| 56 | if(isChecked) { |
| 57 | Utils.setEnabledRecursively(layout, true); |
| 58 | final Spinner repeatType = (Spinner) root.findViewById(R.id.repeat_type); |
| 59 | repeatType.setOnItemSelectedListener(new OnItemSelectedListener(root.findViewById(R.id.repeat_count),root.findViewById(R.id.repeat_until))); |
| 60 | }else { |
| 61 | Utils.setEnabledRecursively(layout, false); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public static final String ARGUMENT_LAYOUT="layout"; |
| 67 | |
| 68 | @Override |
| 69 | public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { |
| 70 | |
| 71 | final View view=inflater.inflate(getArguments().getInt(ARGUMENT_LAYOUT), container, false); |
| 72 | final LinearLayout layout = (LinearLayout) view.findViewById(R.id.repeat_layout); |
| 73 | |
| 74 | if(layout != null){ |
| 75 | final Switch repeatSwitch = (Switch) view.findViewById(R.id.repeat_switch); |
| 76 | Utils.setEnabledRecursively(layout, false); |
| 77 | |
| 78 | repeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(layout, view)); |
| 79 | } |
| 80 | return view; |
| 81 | } |
| 82 | } |