-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from naghtarr/naghtarr/itemclicks
Add OperatorAdapterViewOnItemClick to observe OnItemClick events in AdapterViews. Resolve #19.
- Loading branch information
Showing
6 changed files
with
559 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package rx.android.events; | ||
|
||
import android.view.View; | ||
import android.widget.Adapter; | ||
import android.widget.AdapterView; | ||
|
||
public class OnItemClickEvent { | ||
public final AdapterView<?> parent; | ||
public final View view; | ||
public final int position; | ||
public final long id; | ||
|
||
public OnItemClickEvent(AdapterView<?> parent, View view, int position, long id) { | ||
this.parent = parent; | ||
this.view = view; | ||
this.position = position; | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/rx/android/operators/OperatorAdapterViewOnItemClick.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.android.operators; | ||
|
||
import android.view.View; | ||
import android.widget.AbsListView; | ||
import android.widget.AdapterView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.Subscription; | ||
import rx.android.events.OnItemClickEvent; | ||
import rx.android.observables.Assertions; | ||
import rx.android.subscriptions.AndroidSubscriptions; | ||
import rx.functions.Action0; | ||
|
||
public class OperatorAdapterViewOnItemClick implements Observable.OnSubscribe<OnItemClickEvent> { | ||
|
||
private final AdapterView<?> adapterView; | ||
|
||
public OperatorAdapterViewOnItemClick(final AdapterView<?> adapterView) { | ||
this.adapterView = adapterView; | ||
} | ||
|
||
@Override | ||
public void call(final Subscriber<? super OnItemClickEvent> observer) { | ||
Assertions.assertUiThread(); | ||
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(adapterView); | ||
|
||
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<AdapterView<?>, CompositeOnClickListener> sCachedListeners = new WeakHashMap<AdapterView<?>, CompositeOnClickListener>(); | ||
|
||
public static CompositeOnClickListener getFromViewOrCreate(final AdapterView<?> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.