diff --git a/library/src/main/java/fr/ganfra/materialspinner/MaterialSpinner.java b/library/src/main/java/fr/ganfra/materialspinner/MaterialSpinner.java index 542e576..b7e5896 100644 --- a/library/src/main/java/fr/ganfra/materialspinner/MaterialSpinner.java +++ b/library/src/main/java/fr/ganfra/materialspinner/MaterialSpinner.java @@ -101,6 +101,7 @@ public class MaterialSpinner extends AppCompatSpinner implements ValueAnimator.A private boolean enableFloatingLabel; private boolean alwaysShowFloatingLabel; private boolean isRtl; + private boolean isHintSelectable; private HintAdapter hintAdapter; @@ -183,6 +184,7 @@ private void initAttributes(Context context, AttributeSet attrs) { isRtl = array.getBoolean(R.styleable.MaterialSpinner_ms_isRtl, false); mHintView = array.getResourceId(R.styleable.MaterialSpinner_ms_hintView, android.R.layout.simple_spinner_item); mDropDownHintView = array.getResourceId(R.styleable.MaterialSpinner_ms_dropDownHintView, android.R.layout.simple_spinner_dropdown_item); + isHintSelectable = array.getBoolean(R.styleable.MaterialSpinner_ms_isHintSelectable, true); String typefacePath = array.getString(R.styleable.MaterialSpinner_ms_typeface); if (typefacePath != null) { @@ -385,7 +387,7 @@ private int prepareBottomPadding() { } private boolean isSpinnerEmpty() { - return (hintAdapter.getCount() == 0 && hint == null) || (hintAdapter.getCount() == 1 && hint != null); + return (hintAdapter.getCount() == 0 && (hint == null || !isHintSelectable)) || (hintAdapter.getCount() == 1 && hint != null); } /* @@ -791,6 +793,16 @@ public boolean isRtl() { return isRtl; } + + public void setHintSelectable(boolean isHintSelectable){ + this.isHintSelectable = isHintSelectable; + invalidate(); + } + + public boolean isHintSelectable(){ + return isHintSelectable; + } + /** * @deprecated {use @link #setPaddingSafe(int, int, int, int)} to keep internal computation OK */ @@ -947,7 +959,9 @@ private View buildView(int position, View convertView, ViewGroup parent, boolean } private View getHintView(final View convertView, final ViewGroup parent, final boolean isDropDownView) { - + if(isDropDownView && !isHintSelectable){ + return new View(mContext); + } final LayoutInflater inflater = LayoutInflater.from(mContext); final int resid = isDropDownView ? mDropDownHintView : mHintView; final TextView textView = (TextView) inflater.inflate(resid, parent, false); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 605e5e3..59c6255 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -24,6 +24,7 @@ + \ No newline at end of file diff --git a/sample/src/main/java/fr/ganfra/materialspinner/sample/MainActivity.java b/sample/src/main/java/fr/ganfra/materialspinner/sample/MainActivity.java index 7682a1c..f58ebaa 100644 --- a/sample/src/main/java/fr/ganfra/materialspinner/sample/MainActivity.java +++ b/sample/src/main/java/fr/ganfra/materialspinner/sample/MainActivity.java @@ -2,7 +2,9 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.View; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import fr.ganfra.materialspinner.MaterialSpinner; @@ -22,6 +24,7 @@ public class MainActivity extends AppCompatActivity { MaterialSpinner spinner5; MaterialSpinner spinner6; MaterialSpinner spinner7; + MaterialSpinner spinner8; private boolean shown = false; @@ -43,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) { initSpinnerScrolling(); initSpinnerHintAndCustomHintView(); initEmptyArray(); - + initHintNotSelectable(); } private void initSpinnerHintAndCustomHintView() { @@ -85,6 +88,25 @@ private void initEmptyArray() { spinner7.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, emptyArray)); } + private void initHintNotSelectable(){ + spinner8 = findViewById(R.id.spinner8); + spinner8.setAdapter(adapter); + spinner8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + if(spinner8.getSelectedItem() != null) { + Log.d("SELECTED_ITEM", spinner8.getSelectedItem().toString()); + Log.d("SELECTED_ITEM", ITEMS[i]); + } + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + Log.d("SELECTED_ITEM", "No thing selected"); + } + }); + } + public void activateError(View view) { if (!shown) { spinner4.setError(ERROR_MSG); diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index dde1045..a5eb9ab 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -67,6 +67,13 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + +