zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 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);
    }
}