Skip to content

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

Merged
merged 1 commit into from
Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 +
'}';
}
}
12 changes: 12 additions & 0 deletions rxandroid/src/main/java/rx/android/observables/ViewObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package rx.android.observables;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.TextView;
Expand All @@ -22,9 +23,11 @@
import rx.android.events.OnCheckedChangeEvent;
import rx.android.events.OnClickEvent;
import rx.android.events.OnItemClickEvent;
import rx.android.events.OnListViewScrollEvent;
import rx.android.events.OnTextChangeEvent;
import rx.android.operators.OperatorAdapterViewOnItemClick;
import rx.android.operators.OperatorCompoundButtonInput;
import rx.android.operators.OnSubscribeListViewScroll;
import rx.android.operators.OperatorTextViewInput;
import rx.android.operators.OperatorViewClick;

Expand Down Expand Up @@ -58,4 +61,13 @@ public static Observable<OnItemClickEvent> itemClicks(final AdapterView<?> adapt
return Observable.create(new OperatorAdapterViewOnItemClick(adapterView));
}

/**
* Returns an observable that emits all the scroll events from the provided ListView.
* Note that this will replace any listeners previously set through
* {@link android.widget.AbsListView#setOnScrollListener(android.widget.AbsListView.OnScrollListener)} unless those
* were set by this method or {@link rx.android.operators.OnSubscribeListViewScroll}.
*/
public static Observable<OnListViewScrollEvent> listScrollEvents(final AbsListView listView) {
return Observable.create(new OnSubscribeListViewScroll(listView));
}
}
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() {
Copy link
Collaborator

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?

@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;

Expand All @@ -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;

Expand All @@ -39,18 +47,38 @@ public RetainedListFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
out in a structured way rather than a sprawl.
On Tue, Nov 18, 2014 at 16:41 David Sobreira Marques <
notifications@github.com> wrote:

In sample-app/src/main/java/rx/android/samples/ListFragmentActivity.java:

@@ -39,18 +47,38 @@ public RetainedListFragment() {
}

     @Override
  •    public void onCreate(Bundle savedInstanceState) {
    
  •        super.onCreate(savedInstanceState);
    
  •    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    

@mttkay https://github.com/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?


Reply to this email directly or view it on GitHub
https://github.com/ReactiveX/RxAndroid/pull/57/files#r20540578.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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>() {
Expand Down
18 changes: 9 additions & 9 deletions sample-app/src/main/res/layout/list_fragment.xml
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>