Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Spinner widget open from start-of-list when nothing is selected #1955

Merged
merged 2 commits into from Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,60 @@
package org.odk.collect.android.views;

import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;

/**
* Created by vaibhav on 1/3/18.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace this comment with a description of the class. The very top should have a copyright notice with your name (you are the copyright holder) -- https://github.com/opendatakit/collect/blob/master/LICENSE.md#appendix-how-to-apply-the-apache-license-to-your-work


public class CustomSpinner extends AppCompatSpinner {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would a more descriptive name be? How about ScrolledToTopSpinner or something like that?


private boolean toggleFlag = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also have a more descriptive name. I would call it spinnerClicked and have it start as false. It's confusing that the condition in getSelectedItemPosition is negated.


public CustomSpinner(Context context, AttributeSet attrs,
int defStyle, int mode) {
super(context, attrs, defStyle, mode);
}

public CustomSpinner(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomSpinner(Context context, int mode) {
super(context, mode);
}

public CustomSpinner(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 (!toggleFlag && 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
toggleFlag = false;
boolean result = super.performClick();
toggleFlag = true;
return result;
}

}
Expand Up @@ -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;
Expand All @@ -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.CustomSpinner;
import org.odk.collect.android.widgets.interfaces.MultiChoiceWidget;

import java.util.List;
Expand All @@ -52,7 +52,7 @@
@SuppressLint("ViewConstructor")
public class SpinnerWidget extends QuestionWidget implements MultiChoiceWidget {
List<SelectChoice> items;
Spinner spinner;
CustomSpinner spinner;
String[] choices;

public SpinnerWidget(Context context, FormEntryPrompt prompt) {
Expand Down
2 changes: 1 addition & 1 deletion collect_app/src/main/res/layout/spinner_layout.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatSpinner xmlns:android="http://schemas.android.com/apk/res/android"
<org.odk.collect.android.views.CustomSpinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
Expand Down