Created
January 2, 2019 18:38
-
-
Save mrhether/12998f40d76f2618a20f37a849059ff6 to your computer and use it in GitHub Desktop.
NothingSelectedSpinnerAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.surpriise.vouchrcommon.ui.components; | |
import android.content.Context; | |
import android.database.DataSetObserver; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListAdapter; | |
import android.widget.SpinnerAdapter; | |
import android.widget.TextView; | |
/** | |
* Decorator Adapter to allow a Spinner to show a 'Nothing Selected...' initially | |
* displayed instead of the first choice in the Adapter. | |
*/ | |
/* Source:https://stackoverflow.com/a/12221309/1489782 */ | |
public class NothingSelectedSpinnerAdapter extends ArrayAdapter<String> implements SpinnerAdapter, ListAdapter { | |
protected static final int EXTRA = 1; | |
protected SpinnerAdapter adapter; | |
protected Context context; | |
protected int nothingSelectedLayout; | |
protected int nothingSelectedDropdownLayout; | |
protected LayoutInflater layoutInflater; | |
/** | |
* Use this constructor to have NO 'Select One...' item, instead use | |
* the standard prompt or nothing at all. | |
* @param spinnerAdapter wrapped Adapter. | |
* @param nothingSelectedLayout layout for nothing selected, perhaps | |
* you want text grayed out like a prompt... | |
* @param context | |
*/ | |
public NothingSelectedSpinnerAdapter( | |
SpinnerAdapter spinnerAdapter, | |
int nothingSelectedLayout, Context context) { | |
this(spinnerAdapter, nothingSelectedLayout, -1, context); | |
} | |
/** | |
* Use this constructor to Define your 'Select One...' layout as the first | |
* row in the returned choices. | |
* If you do this, you probably don't want a prompt on your spinner or it'll | |
* have two 'Select' rows. | |
* | |
* @param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0) | |
* @param nothingSelectedLayout layout for nothing selected, perhaps you want | |
* text grayed out like a prompt... | |
* @param nothingSelectedDropdownLayout layout for your 'Select an Item...' in | |
* the dropdown. | |
* @param context | |
*/ | |
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter, | |
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) { | |
super(context, 0); | |
this.adapter = spinnerAdapter; | |
this.context = context; | |
this.nothingSelectedLayout = nothingSelectedLayout; | |
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout; | |
layoutInflater = LayoutInflater.from(context); | |
} | |
@Override | |
public final View getView(int position, View convertView, ViewGroup parent) { | |
// This provides the View for the Selected Item in the Spinner, not | |
// the dropdown (unless dropdownView is not set). | |
if (position == 0) { | |
View view = getNothingSelectedView(parent); | |
view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); | |
return view; | |
} else { | |
TextView textView = (TextView) getNothingSelectedView(parent); | |
textView.setText(((TextView) adapter.getView(position - EXTRA, null, parent)).getText()); | |
textView.setPadding(0, textView.getPaddingTop(), textView.getPaddingRight(), textView.getPaddingBottom()); | |
return textView; | |
} | |
} | |
/** | |
* View to show in Spinner with Nothing Selected | |
* Override this to do something dynamic... e.g. "37 Options Found" | |
* | |
* @param parent | |
* @return | |
*/ | |
protected View getNothingSelectedView(ViewGroup parent) { | |
return layoutInflater.inflate(nothingSelectedLayout, parent, false); | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, ViewGroup parent) { | |
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 - | |
// Spinner does not support multiple view types | |
if (position == 0) { | |
return nothingSelectedDropdownLayout == -1 ? | |
new View(context) : | |
getNothingSelectedDropdownView(parent); | |
} | |
// Could re-use the convertView if possible, use setTag... | |
return adapter.getDropDownView(position - EXTRA, null, parent); | |
} | |
/** | |
* Override this to do something dynamic... For example, "Pick your favorite | |
* of these 37". | |
* | |
* @param parent | |
* @return | |
*/ | |
protected View getNothingSelectedDropdownView(ViewGroup parent) { | |
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false); | |
} | |
@Override | |
public int getCount() { | |
int count = adapter.getCount(); | |
return count == 0 ? 0 : count + EXTRA; | |
} | |
@Override | |
public String getItem(int position) { | |
return position == 0 ? null : (String) adapter.getItem(position - EXTRA); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return 0; | |
} | |
@Override | |
public int getViewTypeCount() { | |
return 1; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA; | |
} | |
@Override | |
public boolean hasStableIds() { | |
return adapter.hasStableIds(); | |
} | |
@Override | |
public boolean isEmpty() { | |
return adapter.isEmpty(); | |
} | |
@Override | |
public void registerDataSetObserver(DataSetObserver observer) { | |
adapter.registerDataSetObserver(observer); | |
} | |
@Override | |
public void unregisterDataSetObserver(DataSetObserver observer) { | |
adapter.unregisterDataSetObserver(observer); | |
} | |
@Override | |
public boolean areAllItemsEnabled() { | |
return false; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return position != 0; // Don't allow the 'nothing selected' | |
// item to be picked. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment