Comments (4)
Could you post some sample code? It sounds like it would be useful to have.
from rxandroid.
Wow @naghtarr nice! I was thinking something along the lines of your commit, but in my case I need it for an AutoCompleteTextView
which surprisingly is not an AdapterView
.
Usage would be almost identical though --
ViewObservable
.itemClicks(autoCompleteTextView)
.subscribe((parent, view, position, id) -> { Log.d(TAG, "Item " + position + " clicked"); });
from rxandroid.
@matthewmichihara It would be very similar for AutoCompleteTextView
:
public class OperatorAutoCompleteTextViewSuggestionsClick implements Observable.OnSubscribe<OnItemClickEvent> {
private final AutoCompleteTextView autoCompleteTextView;
public OperatorAutoCompleteTextViewSuggestionsClick(final AutoCompleteTextView autoCompleteTextView) {
this.autoCompleteTextView = autoCompleteTextView;
}
@Override
public void call(final Subscriber<? super OnItemClickEvent> observer) {
Assertions.assertUiThread();
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(autoCompleteTextView);
final AbsListView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
observer.onNext(new OnItemClickEvent(parent, view, position, id));
}
};
final Subscription subscription = AndroidSubscriptions.unsubscribeInUiThread(new Action0() {
@Override
public void call() {
composite.removeOnClickListener(listener);
}
});
composite.addOnClickListener(listener);
observer.add(subscription);
}
private static class CompositeOnClickListener implements AbsListView.OnItemClickListener {
private final List<AbsListView.OnItemClickListener> listeners = new ArrayList<AbsListView.OnItemClickListener>();
public boolean addOnClickListener(final AbsListView.OnItemClickListener listener) {
return listeners.add(listener);
}
public boolean removeOnClickListener(final AbsListView.OnItemClickListener listener) {
return listeners.remove(listener);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (AdapterView.OnItemClickListener listener : listeners) {
listener.onItemClick(parent, view, position, id);
}
}
}
private static class CachedListeners {
private static final Map<AutoCompleteTextView, CompositeOnClickListener> sCachedListeners = new WeakHashMap<AutoCompleteTextView, CompositeOnClickListener>();
public static CompositeOnClickListener getFromViewOrCreate(final AutoCompleteTextView view) {
final CompositeOnClickListener cached = sCachedListeners.get(view);
if (cached != null) {
return cached;
}
final CompositeOnClickListener listener = new CompositeOnClickListener();
sCachedListeners.put(view, listener);
view.setOnItemClickListener(listener);
return listener;
}
}
}
And at ViewObservable
public static <T extends AutoCompleteTextView> Observable<OnItemClickEvent> itemClicks(final T autoCompleteTextView) {
return Observable.create(new OperatorAutoCompleteTextViewSuggestionsClick(autoCompleteTextView));
}
Unfortunately, AutoCompleteTextView
is untestable. It contains ListPopupWindow
which not always exists. It creates inner DropDownListView every time show()
called. Therefore, fails in unit tests. For example after performing click:
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.shadows.ShadowWindowManagerImpl.removeView(ShadowWindowManagerImpl.java:33)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java)
at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
at org.robolectric.shadows.ShadowPopupWindow.dismiss(ShadowPopupWindow.java:118)
at android.widget.PopupWindow.dismiss(PopupWindow.java)
at android.widget.ListPopupWindow.dismiss(ListPopupWindow.java:612)
at android.widget.AutoCompleteTextView.dismissDropDown(AutoCompleteTextView.java:1018)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:890)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1177)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
at rx.android.operators.OperatorAutoCompleteTextViewSuggestionsClickTest.testAutoCompleteSuggestions(OperatorAutoCompleteTextViewSuggestionsClickTest.java:95)
Even more. ListPopupWindow
is private field. I had to use reflection to get it. So this test is not good, I guess.
from rxandroid.
👍 thanks guys
from rxandroid.
Related Issues (20)
- move from version 2 to version 3 HOT 2
- BUG with gradle 3.6.1 and RxJava and RxAndroid 3.0.0 HOT 2
- RX Android & Release apk HOT 1
- RxAndroid
- HandlerScheduler throws NPE on schedule() in tests HOT 4
- AndroidRuntime: FATAL EXCEPTION -> observeOn(AndroidSchedulers.mainThread()) HOT 5
- Bug or Help Long running observable in background crashes app HOT 1
- [email protected]: Permission denied (publickey). HOT 1
- java.lang.NoSuchMethodError HOT 17
- IllegalAccessError: FlowableDoOnLifecycle: 2.2.3 HOT 1
- docs/content/github/collaborating-with-issues-and-pull-requests/merging-a-pull-request.md
- Gi
- issue with the indirect dependency component reactive-stream License HOT 2
- Does RxAndroid have any plans to release a new version? HOT 1
- Add Sonatype secrets to repo for GitHub Actions usage HOT 9
- RxAndroid 3.0.1 NPE in JVM unit tests HOT 1
- ERROR: Failed to resolve: io.reactivex.rxjava3:rxandroid:3.0.2 HOT 5
- Sms2
- Why the HandlerScheduler class overrides scheduleDirect. It's not more explicit to let the HandlerWorker do the work HOT 4
- Kotlin multiplaform support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxandroid.