|
| 1 | +/* |
| 2 | + * Copyright 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.material.catalog.progressindicator; |
| 17 | + |
| 18 | +import io.material.catalog.R; |
| 19 | + |
| 20 | +import android.os.Bundle; |
| 21 | +import android.view.LayoutInflater; |
| 22 | +import android.view.View; |
| 23 | +import android.view.ViewGroup; |
| 24 | +import android.widget.AutoCompleteTextView; |
| 25 | +import android.widget.Button; |
| 26 | +import androidx.annotation.NonNull; |
| 27 | +import androidx.annotation.Nullable; |
| 28 | +import com.google.android.material.materialswitch.MaterialSwitch; |
| 29 | +import com.google.android.material.progressindicator.CircularProgressIndicator; |
| 30 | +import com.google.android.material.progressindicator.LinearProgressIndicator; |
| 31 | +import com.google.android.material.slider.Slider; |
| 32 | +import io.material.catalog.feature.DemoFragment; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Locale; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +/** |
| 38 | + * This is the fragment to demo different visibility change behaviors of {@link |
| 39 | + * LinearProgressIndicator} and {@link CircularProgressIndicator}. |
| 40 | + */ |
| 41 | +public class ProgressIndicatorVisibilityDemoFragment extends DemoFragment { |
| 42 | + |
| 43 | + public static final int SHOW_NONE = 0; |
| 44 | + public static final int SHOW_OUTWARD = 1; |
| 45 | + public static final int SHOW_INWARD = 2; |
| 46 | + public static final int HIDE_NONE = 0; |
| 47 | + public static final int HIDE_OUTWARD = 1; |
| 48 | + public static final int HIDE_INWARD = 2; |
| 49 | + public static final int HIDE_ESCAPE = 3; |
| 50 | + |
| 51 | + private static final Map<String, Integer> showBehaviorCodes = new HashMap<>(); |
| 52 | + private static final Map<String, Integer> hideBehaviorCodes = new HashMap<>(); |
| 53 | + |
| 54 | + static { |
| 55 | + showBehaviorCodes.put("none", SHOW_NONE); |
| 56 | + showBehaviorCodes.put("outward", SHOW_OUTWARD); |
| 57 | + showBehaviorCodes.put("inward", SHOW_INWARD); |
| 58 | + hideBehaviorCodes.put("none", HIDE_NONE); |
| 59 | + hideBehaviorCodes.put("outward", HIDE_OUTWARD); |
| 60 | + hideBehaviorCodes.put("inward", HIDE_INWARD); |
| 61 | + hideBehaviorCodes.put("escape", HIDE_ESCAPE); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + @NonNull |
| 66 | + public View onCreateDemoView( |
| 67 | + @NonNull LayoutInflater layoutInflater, |
| 68 | + @Nullable ViewGroup viewGroup, |
| 69 | + @Nullable Bundle bundle) { |
| 70 | + |
| 71 | + View view = |
| 72 | + layoutInflater.inflate( |
| 73 | + R.layout.cat_progress_indicator_visibility_fragment, |
| 74 | + viewGroup, |
| 75 | + false /* attachToRoot */); |
| 76 | + |
| 77 | + initialize(view); |
| 78 | + |
| 79 | + return view; |
| 80 | + } |
| 81 | + |
| 82 | + public void initialize(@NonNull View view) { |
| 83 | + LinearProgressIndicator linearIndicator = view.findViewById(R.id.linear_indicator); |
| 84 | + CircularProgressIndicator circularIndicator = view.findViewById(R.id.circular_indicator); |
| 85 | + Slider progressSlider = view.findViewById(R.id.progress_slider); |
| 86 | + MaterialSwitch determinateSwitch = view.findViewById(R.id.determinate_mode_switch); |
| 87 | + |
| 88 | + progressSlider.addOnChangeListener( |
| 89 | + (slider, value, fromUser) -> { |
| 90 | + if (!linearIndicator.isIndeterminate()) { |
| 91 | + linearIndicator.setProgressCompat((int) value, true); |
| 92 | + } |
| 93 | + if (!circularIndicator.isIndeterminate()) { |
| 94 | + circularIndicator.setProgressCompat((int) value, true); |
| 95 | + } |
| 96 | + }); |
| 97 | + determinateSwitch.setOnCheckedChangeListener( |
| 98 | + (v, isChecked) -> { |
| 99 | + if (isChecked) { |
| 100 | + float progress = progressSlider.getValue(); |
| 101 | + linearIndicator.setProgressCompat((int) progress, true); |
| 102 | + circularIndicator.setProgressCompat((int) progress, true); |
| 103 | + } else { |
| 104 | + linearIndicator.setProgressCompat(0, false); |
| 105 | + circularIndicator.setProgressCompat(0, false); |
| 106 | + linearIndicator.setIndeterminate(true); |
| 107 | + circularIndicator.setIndeterminate(true); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + AutoCompleteTextView showBehaviorInput = view.findViewById(R.id.showBehaviorDropdown); |
| 112 | + showBehaviorInput.setOnItemClickListener( |
| 113 | + (parent, view12, position, id) -> { |
| 114 | + String selected = (String) showBehaviorInput.getAdapter().getItem(position); |
| 115 | + int showBehaviorCode = showBehaviorCodes.get(selected.toLowerCase(Locale.US)); |
| 116 | + linearIndicator.setShowAnimationBehavior(showBehaviorCode); |
| 117 | + circularIndicator.setShowAnimationBehavior(showBehaviorCode); |
| 118 | + }); |
| 119 | + AutoCompleteTextView hideBehaviorInput = view.findViewById(R.id.hideBehaviorDropdown); |
| 120 | + hideBehaviorInput.setOnItemClickListener( |
| 121 | + (parent, view1, position, id) -> { |
| 122 | + String selected = (String) hideBehaviorInput.getAdapter().getItem(position); |
| 123 | + int hideBehaviorCode = hideBehaviorCodes.get(selected.toLowerCase(Locale.US)); |
| 124 | + linearIndicator.setHideAnimationBehavior(hideBehaviorCode); |
| 125 | + circularIndicator.setHideAnimationBehavior(hideBehaviorCode); |
| 126 | + }); |
| 127 | + |
| 128 | + Button showButton = view.findViewById(R.id.showButton); |
| 129 | + showButton.setOnClickListener( |
| 130 | + v -> { |
| 131 | + if (linearIndicator.getVisibility() != View.VISIBLE) { |
| 132 | + linearIndicator.show(); |
| 133 | + } |
| 134 | + if (circularIndicator.getVisibility() != View.VISIBLE) { |
| 135 | + circularIndicator.show(); |
| 136 | + } |
| 137 | + }); |
| 138 | + Button hideButton = view.findViewById(R.id.hideButton); |
| 139 | + hideButton.setOnClickListener( |
| 140 | + v -> { |
| 141 | + if (linearIndicator.getVisibility() == View.VISIBLE) { |
| 142 | + linearIndicator.hide(); |
| 143 | + } |
| 144 | + if (circularIndicator.getVisibility() == View.VISIBLE) { |
| 145 | + circularIndicator.hide(); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | +} |
0 commit comments