Skip to content

Commit

Permalink
Don't create popup if detached from window. Bump version (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 authored Apr 19, 2018
1 parent 5a08de3 commit dd3681f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Simple yet powerful autocomplete behavior for `EditText`s, to avoid working with
`MultiAutoCompleteTextView` APIs.

```groovy
implementation 'com.otaliastudios:autocomplete:1.0.3'
implementation 'com.otaliastudios:autocomplete:1.1.0'
```

To see it in action, take a look at the sample app in the `sample` module.
Expand Down Expand Up @@ -107,6 +107,13 @@ The presenter controls the display of items and their filtering when a query is
It is recommended to extend `RecyclerViewPresenter`, which shows a `RecyclerView` list.
For more complex needs, look at the base `AutocompletePresenter` class and its comments.

**Note**: starting from **1.1.0**, if the view returned by `AutocompletePresenter` has 0 height, this is read as a
no-data signal and the popup will be dismissed. Not doing so would cause drawing artifacts, by
leaving the popup in a weird state.

If you are performing asynchronous loading, make sure to give some height to your view,
for example by returning a 'loading' item from your adapter, or adding vertical padding.

#### RecyclerViewPresenter

This automatically inflates a `RecyclerView` into the popup. Some relevant callbacks to be overriden:
Expand Down
2 changes: 1 addition & 1 deletion autocomplete/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

// Required by bintray
version = '1.0.3'
version = '1.1.0'
group = 'com.otaliastudios'

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.Selection;
import android.text.SpanWatcher;
Expand Down Expand Up @@ -173,24 +174,31 @@ private Autocomplete(Builder<T> builder) {

// Set up popup
popup = new AutocompletePopup(source.getContext());
popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popup.setAnchorView(source);
popup.setGravity(Gravity.START);
popup.setModal(false);
popup.setBackgroundDrawable(builder.backgroundDrawable);
popup.setElevation(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, builder.elevationDp,
source.getContext().getResources().getDisplayMetrics()));

// popup dimensions
AutocompletePresenter.PopupDimensions dim = this.presenter.getPopupDimensions();
popup.setWidth(dim.width);
popup.setHeight(dim.height);
popup.setMaxWidth(dim.maxWidth);
popup.setMaxHeight(dim.maxHeight);

// Fire visibility events
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
lastQuery = "null";
if (callback != null) callback.onPopupVisibilityChanged(false);
boolean saved = block;
block = true;
policy.onDismiss(source.getText());
block = saved;
presenter.hideView();
}
});

Expand All @@ -202,14 +210,14 @@ public void onDismiss() {
presenter.registerClickProvider(new AutocompletePresenter.ClickProvider<T>() {
@Override
public void click(T item) {
AutocompleteCallback<T> c = Autocomplete.this.callback;
EditText e = Autocomplete.this.source;
if (c == null) return;
boolean b = block;
AutocompleteCallback<T> callback = Autocomplete.this.callback;
EditText edit = Autocomplete.this.source;
if (callback == null) return;
boolean saved = block;
block = true;
boolean dismiss = c.onPopupItemClicked(e.getText(), item);
boolean dismiss = callback.onPopupItemClicked(edit.getText(), item);
if (dismiss) dismissPopup();
block = b;
block = saved;
}
});

Expand Down Expand Up @@ -244,7 +252,7 @@ public void setSoftInputMode(int mode) {
*
* @param query query text.
*/
public void showPopup(CharSequence query) {
public void showPopup(@NonNull CharSequence query) {
if (isPopupShowing() && lastQuery.equals(query.toString())) return;
lastQuery = query.toString();

Expand All @@ -267,16 +275,8 @@ public void showPopup(CharSequence query) {
* To control when this is called, provide a good implementation of {@link AutocompletePolicy}.
*/
public void dismissPopup() {
lastQuery = "null";
if (isPopupShowing()) {
log("dismissPopup: called, and popup is showing");
popup.dismiss();
boolean b = block;
block = true;
policy.onDismiss(source.getText());
block = b;
presenter.hideView();
// Not calling onPopupVisibilityChanged. That is done with a OnDismissListener.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void setAnimationStyle(@StyleRes int animationStyle) {
* Returns the view that will be used to anchor this popup.
* @return The popup's anchor view
*/
@Nullable View getAnchorView() {
View getAnchorView() {
return mAnchorView;
}

Expand All @@ -183,7 +183,7 @@ void setAnimationStyle(@StyleRes int animationStyle) {
* the anchor view when shown.
* @param anchor The view to use as an anchor.
*/
void setAnchorView(@Nullable View anchor) {
void setAnchorView(@NonNull View anchor) {
mAnchorView = anchor;
}

Expand Down Expand Up @@ -294,8 +294,9 @@ void setOnDismissListener(PopupWindow.OnDismissListener listener) {
* will recalculate the popup's size and position.
*/
void show() {
int height = buildDropDown();
if (!ViewCompat.isAttachedToWindow(getAnchorView())) return;

int height = buildDropDown();
final boolean noInputMethod = isInputMethodNotNeeded();
int mDropDownWindowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
PopupWindowCompat.setWindowLayoutType(mPopup, mDropDownWindowLayoutType);
Expand Down
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
}

dependencies {
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:design:$supportLibVersion"
compile project(':autocomplete')
implementation "com.android.support:appcompat-v7:$supportLibVersion"
implementation "com.android.support:design:$supportLibVersion"
implementation project(':autocomplete')
}

0 comments on commit dd3681f

Please sign in to comment.