Created
January 12, 2018 15:28
-
-
Save briansalvattore/23eeb6e751a900fe96b9d9c928a4f54f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.support.annotation.Nullable; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Spinner; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* @author Brian Salvattore | |
*/ | |
@SuppressWarnings("unused") | |
@SuppressLint("AppCompatCustomView") | |
public class MasterSpinner extends Spinner { | |
private ArrayList<HashMap<String, String>> array; | |
private List<String> descriptions; | |
//region Constructor | |
public MasterSpinner(Context context) { | |
super(context); | |
init(); | |
} | |
public MasterSpinner(Context context, int mode) { | |
super(context, mode); | |
init(); | |
} | |
public MasterSpinner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public MasterSpinner(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
public MasterSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) { | |
super(context, attrs, defStyleAttr, mode); | |
init(); | |
} | |
//endregion | |
private void init() { | |
setEnabled(false); | |
setAdapter(new ArrayList<>() , "Seleccione una opción"); | |
} | |
@SuppressWarnings("Convert2streamapi") | |
public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) { | |
if (array == null) throw new NullPointerException("spinner adapter is null"); | |
setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@SuppressWarnings("ConstantConditions") | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
if (i != descriptions.size()) { | |
String description = descriptions.get(i); | |
for (HashMap<String, String> item : array) { | |
if (description.equals(item.get("description"))) { | |
listener.onItemSelected(item); | |
return; | |
} | |
} | |
} | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
}); | |
} | |
@Override | |
public HashMap<String, String> getSelectedItem() { | |
return getSelectedItemPosition() == array.size() ? new HashMap<>() : array.get(getSelectedItemPosition()); | |
} | |
public String getSelectedDescription() { | |
return getSelectedItem().get("description") == null ? "" : getSelectedItem().get("description"); | |
} | |
public void setAdapter(String method) { | |
setAdapter(method, "Seleccione una opción"); | |
} | |
public void setAdapter(ArrayList<HashMap<String, String>> array) { | |
setAdapter(array, "Seleccione una opción"); | |
} | |
@SuppressWarnings("Convert2streamapi") | |
public void setAdapter(ArrayList<HashMap<String, String>> array, String hint) { | |
this.array = array; | |
List<String> descriptions = new ArrayList<>(); | |
for (HashMap<String, String> item : array) { | |
descriptions.add(item.get("description")); | |
} | |
descriptions.add(hint); | |
this.descriptions = descriptions; | |
DefaultArrayAdapter adapter = new DefaultArrayAdapter<>(getContext(), descriptions); | |
setAdapter(adapter); | |
setEnabled(array.size() != 0); | |
setSelection(descriptions.size() == 1 ? 0 : adapter.getCount()); | |
} | |
public void setArray(ArrayList<HashMap<String, String>> array) { | |
this.array = array; | |
} | |
public ArrayList<HashMap<String, String>> getArray() { | |
return array; | |
} | |
@SuppressWarnings("Convert2streamapi") | |
public void setSelection(String description) { | |
if (description != null) { | |
for (int i = 0; i < array.size(); i++) { | |
HashMap<String, String> item = array.get(i); | |
if (description.equals(item.get("description"))) { | |
setSelection(i); | |
return; | |
} | |
} | |
} | |
} | |
public interface OnItemSelectedListener { | |
void onItemSelected(HashMap<String, String> item); | |
} | |
public static class DefaultArrayAdapter<T> extends ArrayAdapter<T> { | |
public DefaultArrayAdapter(Context context, List<T> objects) { | |
super(context, android.R.layout.simple_spinner_dropdown_item, objects); | |
} | |
@Override | |
public int getCount() { | |
int count = super.getCount(); | |
return count == 1 ? count : count - 1; | |
} | |
} | |
//region go to top | |
private boolean toggleFlag = true; | |
@Override | |
public int getSelectedItemPosition() { | |
if (!toggleFlag) return 0; | |
return super.getSelectedItemPosition(); | |
} | |
@Override | |
public boolean performClick() { | |
toggleFlag = false; | |
boolean result = super.performClick(); | |
toggleFlag = true; | |
return result; | |
} | |
//endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment