From d1b56852d1a15981b89bb3e967c9df5907c48544 Mon Sep 17 00:00:00 2001 From: Vaibhav Maheshwari Date: Sat, 3 Mar 2018 05:06:56 +0530 Subject: [PATCH] Make Spinner widget open from start-of-list when nothing is selected (#1955) --- .../android/views/ScrolledToTopSpinner.java | 77 +++++++++++++++++++ .../android/widgets/SpinnerWidget.java | 4 +- .../src/main/res/layout/spinner_layout.xml | 2 +- 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 collect_app/src/main/java/org/odk/collect/android/views/ScrolledToTopSpinner.java diff --git a/collect_app/src/main/java/org/odk/collect/android/views/ScrolledToTopSpinner.java b/collect_app/src/main/java/org/odk/collect/android/views/ScrolledToTopSpinner.java new file mode 100644 index 00000000000..f09df3d4570 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/views/ScrolledToTopSpinner.java @@ -0,0 +1,77 @@ +package org.odk.collect.android.views; + +import android.content.Context; +import android.support.v7.widget.AppCompatSpinner; +import android.util.AttributeSet; + +/** + * Copyright 2018 Vaibhav Maheshwari + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ScrolledToTopSpinner is used in SpinnerWidget so that the spinner scrolls to top + * when opened for first time or when last item is selected (which is remove response) + */ + +public class ScrolledToTopSpinner extends AppCompatSpinner { + + private boolean spinnerClicked = false; + + public ScrolledToTopSpinner(Context context, AttributeSet attrs, + int defStyle, int mode) { + super(context, attrs, defStyle, mode); + } + + public ScrolledToTopSpinner(Context context, AttributeSet attrs, + int defStyle) { + super(context, attrs, defStyle); + } + + public ScrolledToTopSpinner(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ScrolledToTopSpinner(Context context, int mode) { + super(context, mode); + } + + public ScrolledToTopSpinner(Context context) { + super(context); + } + + @Override + public int getSelectedItemPosition() { + // this toggle is required because this method will get called in other + // places too, the most important being called for the + // OnItemSelectedListener + if (spinnerClicked && super.getSelectedItemPosition() == getAdapter().getCount() - 1) { + return 0; // get us to the first element + } + return super.getSelectedItemPosition(); + } + + @Override + public boolean performClick() { + // this method shows the list of elements from which to select one. + // we have to make the getSelectedItemPosition to return 0 so you can + // fool the Spinner and let it think that the selected item is the first + // element + spinnerClicked = true; + boolean result = super.performClick(); + spinnerClicked = false; + return result; + } + +} \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/SpinnerWidget.java b/collect_app/src/main/java/org/odk/collect/android/widgets/SpinnerWidget.java index 8b76428e4b6..b4a947fea1f 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/SpinnerWidget.java +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/SpinnerWidget.java @@ -26,7 +26,6 @@ import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; -import android.widget.Spinner; import android.widget.TextView; import org.javarosa.core.model.SelectChoice; @@ -38,6 +37,7 @@ import org.odk.collect.android.R; import org.odk.collect.android.application.Collect; import org.odk.collect.android.external.ExternalDataUtil; +import org.odk.collect.android.views.ScrolledToTopSpinner; import org.odk.collect.android.widgets.interfaces.MultiChoiceWidget; import java.util.List; @@ -52,7 +52,7 @@ @SuppressLint("ViewConstructor") public class SpinnerWidget extends QuestionWidget implements MultiChoiceWidget { List items; - Spinner spinner; + ScrolledToTopSpinner spinner; String[] choices; public SpinnerWidget(Context context, FormEntryPrompt prompt) { diff --git a/collect_app/src/main/res/layout/spinner_layout.xml b/collect_app/src/main/res/layout/spinner_layout.xml index 2160699afdb..8212c6b78d9 100644 --- a/collect_app/src/main/res/layout/spinner_layout.xml +++ b/collect_app/src/main/res/layout/spinner_layout.xml @@ -1,5 +1,5 @@ -