Skip to content

Commit 763617d

Browse files
committed
Improve show dialog suggestion
1 parent 8f47dab commit 763617d

File tree

4 files changed

+113
-20
lines changed

4 files changed

+113
-20
lines changed

lib-n-ide/src/main/java/com/duy/ide/editor/internal/suggestion/OnSuggestItemClickListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
import com.duy.ide.editor.view.IEditAreaView;
77

88
public interface OnSuggestItemClickListener {
9-
void onClickSuggest(@NonNull IEditAreaView editAreaView, @NonNull SuggestItem item);
9+
void onClickSuggest(@NonNull IEditAreaView editAreaView, int position, @NonNull SuggestItem item);
1010
}

lib-n-ide/src/main/java/com/duy/ide/editor/internal/suggestion/SuggestionAdapter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10+
import android.widget.AdapterView;
1011
import android.widget.ArrayAdapter;
1112
import android.widget.Filter;
1213
import android.widget.TextView;
@@ -30,7 +31,7 @@ public class SuggestionAdapter extends ArrayAdapter<SuggestItem> {
3031
private List<SuggestItem> mFilteredData;
3132

3233
@Nullable
33-
private OnSuggestItemClickListener mListener;
34+
private AdapterView.OnItemClickListener mListener;
3435

3536
private Filter mSuggestionFilter = new Filter() {
3637
@Override
@@ -82,7 +83,7 @@ public SuggestionAdapter(@NonNull Context context,
8283

8384
@NonNull
8485
@Override
85-
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
86+
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
8687
if (convertView == null) {
8788
convertView = mInflater.inflate(R.layout.list_item_suggest_default, null);
8889
}
@@ -97,6 +98,14 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
9798
txtReturnType.setText(ensureNotNull(item.getReturnType()));
9899
txtHeader.setText(ensureNotNull(String.valueOf(item.getTypeHeader())));
99100
}
101+
convertView.setOnClickListener(new View.OnClickListener() {
102+
@Override
103+
public void onClick(View v) {
104+
if (mListener != null) {
105+
mListener.onItemClick(null, v, position, View.NO_ID);
106+
}
107+
}
108+
});
100109
return convertView;
101110
}
102111

@@ -129,7 +138,7 @@ public List<SuggestItem> getAllItems() {
129138
return mOriginData;
130139
}
131140

132-
public void setListener(OnSuggestItemClickListener listener) {
141+
public void setListener(AdapterView.OnItemClickListener listener) {
133142
this.mListener = listener;
134143
}
135144

lib-n-ide/src/main/java/com/duy/ide/editor/view/SuggestionEditor.java

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5-
import android.graphics.Color;
65
import android.graphics.Rect;
76
import android.graphics.drawable.ColorDrawable;
87
import android.os.Build;
@@ -12,6 +11,7 @@
1211
import android.support.v7.widget.ListPopupWindow;
1312
import android.text.Layout;
1413
import android.util.AttributeSet;
14+
import android.view.KeyEvent;
1515
import android.view.View;
1616
import android.view.ViewGroup;
1717
import android.view.WindowManager;
@@ -22,29 +22,30 @@
2222
import com.duy.ide.code.api.SuggestItem;
2323
import com.duy.ide.editor.internal.suggestion.OnSuggestItemClickListener;
2424
import com.duy.ide.editor.internal.suggestion.SuggestionAdapter;
25+
import com.duy.ide.editor.theme.model.EditorTheme;
2526

2627
import java.util.List;
2728

28-
public class SuggestionEditor extends EditActionSupportEditor
29-
implements OnSuggestItemClickListener {
29+
public class SuggestionEditor extends EditActionSupportEditor {
3030

3131
private static final String TAG = "SuggestionEditor";
3232
private final Rect mTmpRect = new Rect();
3333
@Nullable
3434
private SuggestionAdapter mAdapter;
3535
@Nullable
3636
private OnSuggestItemClickListener mOnSuggestItemClickListener;
37+
private ListPopupWindow mPopup;
38+
3739
private AdapterView.OnItemClickListener mSuggestClickListener
3840
= new AdapterView.OnItemClickListener() {
3941
@Override
4042
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
4143
if (mAdapter != null) {
42-
SuggestItem description = mAdapter.getAllItems().get(position);
43-
onClickSuggest(SuggestionEditor.this, description);
44+
performCompletion(position);
4445
}
4546
}
4647
};
47-
private ListPopupWindow mPopup;
48+
4849
@IdRes
4950
private int mDropDownAnchorId = View.NO_ID;
5051

@@ -68,17 +69,20 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
6869
updateCharHeight();
6970
mPopup = new ListPopupWindow(context, attrs, defStyleAttr, 0);
7071
mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
71-
mPopup.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
72+
if (getEditorTheme() != null) {
73+
mPopup.setBackgroundDrawable(new ColorDrawable(getEditorTheme().getBgColor()));
74+
}
7275
}
7376

7477
@Override
75-
public void onClickSuggest(@NonNull IEditAreaView editAreaView,
76-
@NonNull SuggestItem item) {
77-
if (mOnSuggestItemClickListener != null) {
78-
mOnSuggestItemClickListener.onClickSuggest(this, item);
78+
public void setTheme(@NonNull EditorTheme editorTheme) {
79+
super.setTheme(editorTheme);
80+
if (mPopup != null) {
81+
mPopup.setBackgroundDrawable(new ColorDrawable(editorTheme.getBgColor()));
7982
}
8083
}
8184

85+
8286
@Override
8387
public void onTextChanged(CharSequence s, int start, int before, int count) {
8488
super.onTextChanged(s, start, before, count);
@@ -191,7 +195,7 @@ public void setSuggestData(@NonNull List<SuggestItem> data) {
191195
mAdapter.clearAllData();
192196
} else {
193197
mAdapter = new SuggestionAdapter(getContext(), data);
194-
mAdapter.setListener(this);
198+
mAdapter.setListener(mSuggestClickListener);
195199
mPopup.setAdapter(mAdapter);
196200
}
197201
mAdapter.setData(data);
@@ -389,4 +393,80 @@ protected boolean setFrame(final int l, int t, final int r, int b) {
389393
return result;
390394
}
391395

396+
@Override
397+
public boolean onKeyUp(int keyCode, KeyEvent event) {
398+
boolean consumed = mPopup.onKeyUp(keyCode, event);
399+
if (consumed) {
400+
switch (keyCode) {
401+
// if the list accepts the key events and the key event
402+
// was a click, the text view gets the selected item
403+
// from the drop down as its content
404+
case KeyEvent.KEYCODE_ENTER:
405+
case KeyEvent.KEYCODE_DPAD_CENTER:
406+
case KeyEvent.KEYCODE_TAB:
407+
if (event.hasNoModifiers()) {
408+
performCompletion();
409+
}
410+
return true;
411+
}
412+
}
413+
414+
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
415+
performCompletion();
416+
return true;
417+
}
418+
419+
return super.onKeyUp(keyCode, event);
420+
}
421+
422+
@SuppressLint("RestrictedApi")
423+
private void performCompletion() {
424+
performCompletion(-1);
425+
}
426+
427+
@SuppressLint("RestrictedApi")
428+
private void performCompletion(int position) {
429+
if (isPopupShowing() && mAdapter != null) {
430+
if (position < 0) {
431+
position = mPopup.getSelectedItemPosition();
432+
}
433+
if (position < 0) {
434+
return;
435+
}
436+
SuggestItem item = mAdapter.getItem(position);
437+
if (mOnSuggestItemClickListener != null) {
438+
//noinspection ConstantConditions
439+
mOnSuggestItemClickListener.onClickSuggest(this, position, item);
440+
}
441+
}
442+
443+
if (!mPopup.isDropDownAlwaysVisible()) {
444+
dismissDropDown();
445+
}
446+
}
447+
448+
@Override
449+
public boolean onKeyDown(int keyCode, KeyEvent event) {
450+
if (mPopup.onKeyDown(keyCode, event)) {
451+
return true;
452+
}
453+
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
454+
return true;
455+
}
456+
boolean handled = super.onKeyDown(keyCode, event);
457+
458+
if (handled && isPopupShowing()) {
459+
clearListSelection();
460+
}
461+
462+
return handled;
463+
}
464+
465+
/**
466+
* <p>Clear the list selection. This may only be temporary, as user input will often bring
467+
* it back.
468+
*/
469+
public void clearListSelection() {
470+
mPopup.clearListSelection();
471+
}
392472
}

lib-n-ide/src/main/res-file-expoler/layout/list_item_suggest_default.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
android:background="?android:windowBackground"
2222
android:foreground="?selectableItemBackground"
2323
android:gravity="center_vertical"
24-
24+
android:minHeight="25dp"
2525
android:orientation="horizontal">
2626

2727
<android.support.v7.widget.AppCompatTextView
@@ -37,13 +37,15 @@
3737

3838
<android.support.v7.widget.AppCompatTextView
3939
android:id="@+id/txt_name"
40-
android:layout_width="match_parent"
40+
android:layout_width="0dp"
4141
android:layout_height="wrap_content"
4242
android:layout_weight="1"
4343
android:ellipsize="middle"
4444
android:gravity="center|start"
45-
android:padding="2dp"
46-
android:singleLine="true"
45+
android:paddingBottom="4dp"
46+
android:paddingLeft="2dp"
47+
android:paddingRight="2dp"
48+
android:paddingTop="4dp"
4749
android:textColor="?android:textColorPrimary"
4850
android:typeface="monospace"
4951
tools:text="GetResultCode" />
@@ -54,6 +56,8 @@
5456
android:layout_height="wrap_content"
5557
android:gravity="center|end"
5658
android:maxEms="5"
59+
android:paddingBottom="4dp"
60+
android:paddingTop="4dp"
5761
android:typeface="monospace"
5862
tools:text="String" />
5963

0 commit comments

Comments
 (0)