-
Notifications
You must be signed in to change notification settings - Fork 3k
Add ViewObservables.listViewScroll(AbsListView). #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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.events; | ||
|
||
import android.widget.AbsListView; | ||
|
||
public class OnListViewScrollEvent { | ||
public final AbsListView listView; | ||
public final int scrollState; | ||
public final int firstVisibleItem; | ||
public final int visibleItemCount; | ||
public final int totalItemCount; | ||
|
||
public OnListViewScrollEvent( | ||
AbsListView listView, int scrollState, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | ||
this.listView = listView; | ||
this.scrollState = scrollState; | ||
this.firstVisibleItem = firstVisibleItem; | ||
this.visibleItemCount = visibleItemCount; | ||
this.totalItemCount = totalItemCount; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
OnListViewScrollEvent that = (OnListViewScrollEvent) o; | ||
|
||
if (firstVisibleItem != that.firstVisibleItem) { | ||
return false; | ||
} | ||
if (scrollState != that.scrollState) { | ||
return false; | ||
} | ||
if (totalItemCount != that.totalItemCount) { | ||
return false; | ||
} | ||
if (visibleItemCount != that.visibleItemCount) { | ||
return false; | ||
} | ||
if (!listView.equals(that.listView)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = listView.hashCode(); | ||
result = 31 * result + scrollState; | ||
result = 31 * result + firstVisibleItem; | ||
result = 31 * result + visibleItemCount; | ||
result = 31 * result + totalItemCount; | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OnListViewScrollEvent{" + | ||
"listView=" + listView + | ||
", scrollState=" + scrollState + | ||
", firstVisibleItem=" + firstVisibleItem + | ||
", visibleItemCount=" + visibleItemCount + | ||
", totalItemCount=" + totalItemCount + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* 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.widget.AbsListView; | ||
import android.widget.AdapterView; | ||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.android.events.OnListViewScrollEvent; | ||
import rx.android.observables.Assertions; | ||
import rx.android.subscriptions.AndroidSubscriptions; | ||
import rx.functions.Action0; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
public class OnSubscribeListViewScroll implements Observable.OnSubscribe<OnListViewScrollEvent> { | ||
|
||
private final AbsListView listView; | ||
|
||
public OnSubscribeListViewScroll(AbsListView listView) { | ||
this.listView = listView; | ||
} | ||
|
||
@Override | ||
public void call(final Subscriber<? super OnListViewScrollEvent> observer) { | ||
Assertions.assertUiThread(); | ||
|
||
final CompositeOnScrollListener composite = CachedListeners.getFromViewOrCreate(listView); | ||
final AbsListView.OnScrollListener listener = new AbsListView.OnScrollListener() { | ||
int currentScrollState = SCROLL_STATE_IDLE; | ||
|
||
@Override | ||
public void onScrollStateChanged(AbsListView view, int scrollState) { | ||
this.currentScrollState = scrollState; | ||
} | ||
|
||
@Override | ||
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | ||
OnListViewScrollEvent event = new OnListViewScrollEvent(view, this.currentScrollState, firstVisibleItem, | ||
visibleItemCount, totalItemCount); | ||
observer.onNext(event); | ||
} | ||
}; | ||
|
||
composite.addOnScrollListener(listener); | ||
observer.add(AndroidSubscriptions.unsubscribeInUiThread(new Action0() { | ||
@Override | ||
public void call() { | ||
composite.removeOnScrollListener(listener); | ||
} | ||
})); | ||
} | ||
|
||
private static class CompositeOnScrollListener implements AbsListView.OnScrollListener { | ||
|
||
private final List<AbsListView.OnScrollListener> listeners = new ArrayList<AbsListView.OnScrollListener>(); | ||
|
||
public boolean addOnScrollListener(final AbsListView.OnScrollListener listener) { | ||
return listeners.add(listener); | ||
} | ||
|
||
public boolean removeOnScrollListener(final AbsListView.OnScrollListener listener) { | ||
return listeners.remove(listener); | ||
} | ||
|
||
@Override | ||
public void onScrollStateChanged(AbsListView view, int scrollState) { | ||
for (AbsListView.OnScrollListener listener : listeners) { | ||
listener.onScrollStateChanged(view, scrollState); | ||
} | ||
} | ||
|
||
@Override | ||
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | ||
for (AbsListView.OnScrollListener listener : listeners) { | ||
listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); | ||
} | ||
} | ||
} | ||
|
||
private static class CachedListeners { | ||
|
||
private static final Map<AdapterView<?>, CompositeOnScrollListener> sCachedListeners = | ||
new WeakHashMap<AdapterView<?>, CompositeOnScrollListener>(); | ||
|
||
public static CompositeOnScrollListener getFromViewOrCreate(final AbsListView view) { | ||
final CompositeOnScrollListener cached = sCachedListeners.get(view); | ||
if (cached != null) { | ||
return cached; | ||
} | ||
|
||
final CompositeOnScrollListener listener = new CompositeOnScrollListener(); | ||
|
||
sCachedListeners.put(view, listener); | ||
view.setOnScrollListener(listener); | ||
|
||
return listener; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
package rx.android.samples; | ||
|
||
import android.app.Activity; | ||
import android.app.ListFragment; | ||
import android.app.Fragment; | ||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ArrayAdapter; | ||
|
||
import android.widget.ListView; | ||
import android.widget.ProgressBar; | ||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.android.events.OnListViewScrollEvent; | ||
import rx.android.observables.AndroidObservable; | ||
import rx.android.observables.ViewObservable; | ||
import rx.functions.Action1; | ||
|
||
import static rx.android.schedulers.AndroidSchedulers.mainThread; | ||
|
||
|
@@ -30,7 +38,7 @@ protected void onCreate(Bundle savedInstanceState) { | |
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
public static class RetainedListFragment extends ListFragment { | ||
public static class RetainedListFragment extends Fragment { | ||
|
||
private ArrayAdapter<String> adapter; | ||
|
||
|
@@ -39,18 +47,38 @@ public RetainedListFragment() { | |
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mttkay should we define what kinds of things will go into the sample so that we have a cohesive sample application? I wonder if having some sort of "theme" around the sample app would be a good idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree completely on the theme around the sample or at least Building it
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree. It's more for "historic reasons" that the samples are just a "bunch of stuff". Let's collect thoughts on this here: #59 |
||
View view = inflater.inflate(R.layout.list_fragment, container, false); | ||
|
||
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1); | ||
setListAdapter(adapter); | ||
SampleObservables.numberStrings(1, 20, 250) | ||
.observeOn(mainThread()) | ||
.lift(new BindAdapter()) | ||
.subscribe(); | ||
ListView listView = (ListView) view.findViewById(android.R.id.list); | ||
listView.setAdapter(adapter); | ||
|
||
AndroidObservable.bindFragment(this, SampleObservables.numberStrings(1, 500, 100)) | ||
.observeOn(mainThread()) | ||
.lift(new BindAdapter()) | ||
.subscribe(); | ||
|
||
final ProgressBar progressBar = (ProgressBar) view.findViewById(android.R.id.progress); | ||
AndroidObservable.bindFragment(this, ViewObservable.listScrollEvents(listView)) | ||
.subscribe(new Action1<OnListViewScrollEvent>() { | ||
@Override | ||
public void call(OnListViewScrollEvent event) { | ||
if (event.totalItemCount == 0) { | ||
return; | ||
} | ||
|
||
int progress = | ||
(int) ((100.0 * (event.firstVisibleItem + event.visibleItemCount)) / event.totalItemCount); | ||
progressBar.setProgress(progress); | ||
} | ||
}); | ||
|
||
return view; | ||
} | ||
|
||
private final class BindAdapter implements Observable.Operator<String, String> { | ||
|
||
@Override | ||
public Subscriber<? super String> call(Subscriber<? super String> subscriber) { | ||
return new Subscriber<String>() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<TextView | ||
android:id="@android:id/empty" | ||
<ProgressBar | ||
android:id="@android:id/progress" | ||
style="@android:style/Widget.Holo.ProgressBar.Horizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:text="Loading..." | ||
android:visibility="gone" /> | ||
android:layout_height="48dp" | ||
android:indeterminate="false" /> | ||
|
||
<ListView | ||
android:id="@android:id/list" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
android:layout_height="match_parent" | ||
android:layout_marginTop="48dp" /> | ||
|
||
</RelativeLayout> | ||
</FrameLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always thought these double dispatches are weird. We're already testing as a precondition that we're on the main thread, so unless we're rescheduling the observable purposefully to deliver notifications on a background thread after this operator is applied, we will also unsubscribe on the main thread.
I've seen this being done a lot in the last few PRs and always wondered if we're just being defensive or if this will not simply cause an extra cycle through the message loop in the normal case, thus unnecessarily delaying unsubscription?