Skip to content

Commit

Permalink
Add support for Spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Jan 8, 2017
1 parent 5d7208d commit 8e5821b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.RatingBar;
import android.widget.SeekBar;
Expand All @@ -13,6 +14,7 @@
import com.commit451.untriggered.UntriggeredRadioButton;
import com.commit451.untriggered.UntriggeredRatingBar;
import com.commit451.untriggered.UntriggeredSeekBar;
import com.commit451.untriggered.UntriggeredSpinner;
import com.commit451.untriggered.UntriggeredSwitchCompat;

public class MainActivity extends AppCompatActivity {
Expand All @@ -27,7 +29,8 @@ protected void onCreate(Bundle savedInstanceState) {
final UntriggeredRadioButton radioButton = (UntriggeredRadioButton) findViewById(R.id.radio);
final UntriggeredSwitchCompat switchCompat = (UntriggeredSwitchCompat) findViewById(R.id.switch_thing);
final UntriggeredRatingBar ratingBar = (UntriggeredRatingBar) findViewById(R.id.ratingbar);
final UntriggeredSeekBar seekBar= (UntriggeredSeekBar) findViewById(R.id.seekbar);
final UntriggeredSeekBar seekBar = (UntriggeredSeekBar) findViewById(R.id.seekbar);
final UntriggeredSpinner spinner = (UntriggeredSpinner) findViewById(R.id.spinner);

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
Expand Down Expand Up @@ -74,6 +77,18 @@ public void onStopTrackingTouch(SeekBar seekBar) {

}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(root, "Spinner set to " + position + " and I am triggered!", Snackbar.LENGTH_SHORT)
.show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -82,6 +97,7 @@ public void onClick(View v) {
switchCompat.setCheckedUntriggered(!switchCompat.isChecked());
seekBar.setProgressUntriggered(50);
ratingBar.setRatingUntriggered(2.5f);
spinner.setSelectionUntriggered(1, true);
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.commit451.untriggered.UntriggeredSpinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/fruits"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">Untriggered</string>
<string-array name="fruits">
<item>Apple</item>
<item>Orange</item>
<item>Banana</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.commit451.untriggered;

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

/**
* {@link AppCompatSpinner} that allows you to set the check state without triggering the
* {@link android.widget.CompoundButton.OnCheckedChangeListener}
*/
public class UntriggeredSpinner extends AppCompatSpinner {

OnItemSelectedListener onItemSelectedListener;

public UntriggeredSpinner(Context context) {
super(context);
}

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

public UntriggeredSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
super.setOnItemSelectedListener(onItemSelectedListener);
this.onItemSelectedListener = onItemSelectedListener;
}

/**
* Set the selection without triggering the registered {@link android.widget.AdapterView.OnItemSelectedListener}
* @param selection selection
* @param animate if you want it to animate
* @see {@link #setSelection(int, boolean)}
*/
public void setSelectionUntriggered(int selection, boolean animate) {
OnItemSelectedListener currentListener = onItemSelectedListener;
setOnItemSelectedListener(null);
setSelection(selection, animate);
setOnItemSelectedListener(currentListener);
}
}

0 comments on commit 8e5821b

Please sign in to comment.