55import android .graphics .Rect ;
66import android .graphics .drawable .ColorDrawable ;
77import android .os .Build ;
8- import android .support .annotation .IdRes ;
98import android .support .annotation .NonNull ;
109import android .support .annotation .Nullable ;
1110import android .support .v7 .widget .ListPopupWindow ;
2423import com .duy .ide .editor .internal .suggestion .SuggestionAdapter ;
2524import com .duy .ide .editor .theme .model .EditorTheme ;
2625
26+ import java .util .ArrayList ;
2727import java .util .List ;
2828
2929public class SuggestionEditor extends EditActionSupportEditor {
3030
3131 private static final String TAG = "SuggestionEditor" ;
3232 private final Rect mTmpRect = new Rect ();
33- @ Nullable
33+
3434 private SuggestionAdapter mAdapter ;
3535 @ Nullable
3636 private OnSuggestItemClickListener mOnSuggestItemClickListener ;
37+
38+ /**
39+ * Small popup window to display list suggestion
40+ */
3741 private ListPopupWindow mPopup ;
42+ private boolean mSuggestionEnable = false ;
3843
3944 private AdapterView .OnItemClickListener mSuggestClickListener
4045 = new AdapterView .OnItemClickListener () {
@@ -46,9 +51,6 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
4651 }
4752 };
4853
49- @ IdRes
50- private int mDropDownAnchorId = View .NO_ID ;
51-
5254 public SuggestionEditor (Context context ) {
5355 super (context );
5456 init (context , null , 0 );
@@ -66,19 +68,27 @@ public SuggestionEditor(Context context, AttributeSet attrs, int defStyleAttr) {
6668 }
6769
6870 private void init (Context context , AttributeSet attrs , int defStyleAttr ) {
69- updateCharHeight ();
71+ mAdapter = new SuggestionAdapter (getContext (), new ArrayList <SuggestItem >());
72+ mAdapter .setListener (mSuggestClickListener );
73+
7074 mPopup = new ListPopupWindow (context , attrs , defStyleAttr , 0 );
7175 mPopup .setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_ADJUST_RESIZE );
76+ mPopup .setAdapter (mAdapter );
7277 if (getEditorTheme () != null ) {
73- mPopup . setBackgroundDrawable ( new ColorDrawable ( getEditorTheme (). getBgColor () ));
78+ setThemeForPopup ( getEditorTheme ());
7479 }
7580 }
7681
82+ private void setThemeForPopup (EditorTheme theme ) {
83+ mPopup .setBackgroundDrawable (new ColorDrawable (theme .getDropdownBgColor ()));
84+ mAdapter .setTextColor (theme .getDropdownFgColor ());
85+ }
86+
7787 @ Override
7888 public void setTheme (@ NonNull EditorTheme editorTheme ) {
7989 super .setTheme (editorTheme );
8090 if (mPopup != null ) {
81- mPopup . setBackgroundDrawable ( new ColorDrawable ( editorTheme . getBgColor ()) );
91+ setThemeForPopup ( editorTheme );
8292 }
8393 }
8494
@@ -90,6 +100,9 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
90100 }
91101
92102 protected void onPopupChangePosition () {
103+ if (!mSuggestionEnable ) {
104+ return ;
105+ }
93106 try {
94107 Layout layout = getLayout ();
95108 //not ready
@@ -147,6 +160,9 @@ protected void onDropdownChangeSize() {
147160 }
148161
149162 public void showDropDown () {
163+ if (!mSuggestionEnable ) {
164+ return ;
165+ }
150166 if (!isPopupShowing ()) {
151167 if (hasFocus ()) {
152168 try {
@@ -163,11 +179,7 @@ public void showDropDown() {
163179 */
164180 private void showDropDownImpl () {
165181 if (mPopup .getAnchorView () == null ) {
166- if (mDropDownAnchorId != View .NO_ID ) {
167- mPopup .setAnchorView (getRootView ().findViewById (mDropDownAnchorId ));
168- } else {
169- mPopup .setAnchorView (this );
170- }
182+ mPopup .setAnchorView (this );
171183 }
172184 if (!isPopupShowing ()) {
173185 // Make sure the list does not obscure the IME when shown for the first time.
@@ -180,8 +192,10 @@ private void showDropDownImpl() {
180192
181193 @ Override
182194 public void setSuggestEnable (boolean enable ) {
183- if (!enable ) {
184- mAdapter = null ;
195+ mSuggestionEnable = enable ;
196+ if (mSuggestionEnable ) {
197+ onDropdownChangeSize ();
198+ } else {
185199 dismissDropDown ();
186200 }
187201 }
@@ -191,12 +205,8 @@ public void setSuggestEnable(boolean enable) {
191205 */
192206 @ Override
193207 public void setSuggestData (@ NonNull List <SuggestItem > data ) {
194- if (mAdapter != null ) {
195- mAdapter .clearAllData ();
196- } else {
197- mAdapter = new SuggestionAdapter (getContext (), data );
198- mAdapter .setListener (mSuggestClickListener );
199- mPopup .setAdapter (mAdapter );
208+ if (!mSuggestionEnable ) {
209+ return ;
200210 }
201211 mAdapter .setData (data );
202212 mAdapter .notifyDataSetChanged ();
@@ -206,20 +216,6 @@ public void setSuggestData(@NonNull List<SuggestItem> data) {
206216 }
207217 }
208218
209- @ Override
210- public void setTextSize (int unit , float size ) {
211- float oldTextSize = getTextSize ();
212- super .setTextSize (unit , size );
213- if (oldTextSize != size ) {
214- updateCharHeight ();
215- }
216- }
217-
218- private void updateCharHeight () {
219- // Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
220- // mCharHeight = (int) (fontMetrics.bottom - fontMetrics.top);
221- }
222-
223219 @ Override
224220 public void setOnSuggestItemClickListener (@ Nullable OnSuggestItemClickListener listener ) {
225221 this .mOnSuggestItemClickListener = listener ;
0 commit comments