Skip to content

Instantly share code, notes, and snippets.

View stephanenicolas's full-sized avatar

Stéphane Nicolas stephanenicolas

View GitHub Profile
@Override
public void onActivityCreated( Bundle savedInstanceState ) {
super.onActivityCreated( savedInstanceState );
mainActivity = (MainActivity) getActivity();
buttonStart.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v ) {
onStart( v );
}
@stephanenicolas
stephanenicolas / gist:8511492
Created January 19, 2014 21:55
ViewListener that filters listened classes.
/*
* Copyright 2009 Michael Burton
*
* 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
@stephanenicolas
stephanenicolas / Main.java
Last active May 6, 2017 07:23
Updates an adapter concurrently using Rx-managed Redux
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.subjects.BehaviorSubject;
import java.util.ArrayList;
import java.util.Arrays;
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:10
Good Old Way: MainActivity
public static class MainActivity extends Activity
implements HeadlinesFragment.HeadlineListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
@stephanenicolas
stephanenicolas / HeadlinesFragment.java
Created September 26, 2017 23:12
Good Old Way: Fragment
public class HeadlinesFragment extends ListFragment {
HeadlineListener mCallback;
// Container Activity must implement this interface
public interface HeadlineListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:20
Lambda way: MainActivity
public static class MainActivity extends Activity {
public void onArticleSelected(int position) {
}
}
@stephanenicolas
stephanenicolas / HeadlineListener.java
Created September 26, 2017 23:20
Lambda Way: HeadlineListener
public interface HeadlineListener<T extends Activity> extends Serializable {
public void onArticleSelected(T activity, int position);
}
@stephanenicolas
stephanenicolas / HeadlinesFragment.java
Last active September 27, 2017 00:01
Lambda Way: HeadlinesFragment
public class HeadlinesFragment extends ListFragment {
HeadlineListener mCallback;
public <T> void setHeadlineListener(HeadlineListener<T> listener) {
this.mCallBack = listener;
}
@Override
public void onListItemClick(ListView l, View v, int p, long i) {
// Send the event to the host activity
@stephanenicolas
stephanenicolas / MainActivity.java
Created September 26, 2017 23:23
The Lambda Way: setting the listener
public static class MainActivity extends Activity {
public void bindFragment(HeadlinesFragment fragment) {
fragment.setHeadlineListener(MainActivity::onArticleSelected);
}
public void onArticleSelected(int position) {
...
}
}
@stephanenicolas
stephanenicolas / RxChain.java
Created September 26, 2017 23:35
Using a non static method reference in a Rx chain
just(0)
.map(this::onArticleSelected)
.subscribe(...);