package com.basic.security.widget;
|
|
import android.content.Context;
|
import android.util.AttributeSet;
|
import android.widget.ListPopupWindow;
|
import android.widget.ListView;
|
import android.widget.Spinner;
|
|
import java.lang.reflect.Field;
|
|
public class CustomSpinner extends Spinner {
|
public Selection selection;
|
|
public CustomSpinner(Context context) {
|
this(context, null);
|
}
|
|
public CustomSpinner(Context context, AttributeSet attrs) {
|
this(context, attrs, 0);
|
}
|
|
public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
|
super(context, attrs, defStyle, 0, 1);
|
}
|
|
public void setMeasuredDimension1(int measuredWidth, int measuredHeight) {
|
setMeasuredDimension(measuredWidth, measuredHeight);
|
}
|
|
@Override
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
}
|
|
@Override
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
super.onLayout(changed, left, top, right, bottom);
|
}
|
|
public boolean performClick() {
|
try {
|
Field f = this.getClass().getSuperclass().getDeclaredField("mPopup");
|
f.setAccessible(true);
|
ListPopupWindow listPopupWindow = (ListPopupWindow) f.get(this);
|
listPopupWindow.setModal(false);
|
ListView listView = listPopupWindow.getListView();
|
if (listView != null) {
|
listView.setDivider(null);
|
listView.setDividerHeight(0);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
super.performClick();
|
try {
|
Field f = this.getClass().getSuperclass().getDeclaredField("mPopup");
|
f.setAccessible(true);
|
ListPopupWindow listPopupWindow = (ListPopupWindow) f.get(this);
|
listPopupWindow.setModal(false);
|
ListView listView = listPopupWindow.getListView();
|
if (listView != null) {
|
listView.setDivider(null);
|
listView.setDividerHeight(0);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return true;
|
}
|
|
public void setSelection(int position, boolean animate) {
|
boolean sameSelected = position == getSelectedItemPosition();
|
super.setSelection(position, animate);
|
if (sameSelected) {
|
OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
|
if (onItemSelectedListener != null) {
|
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
|
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
|
}
|
}
|
}
|
|
public void setSelection(int position) {
|
boolean sameSelected = position == getSelectedItemPosition();
|
super.setSelection(position);
|
try {
|
if (sameSelected) {
|
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
|
OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
|
if (onItemSelectedListener != null) {
|
onItemSelectedListener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public interface Selection {
|
void selected(int position);
|
}
|
}
|