Skip to content

Instantly share code, notes, and snippets.

Avatar

Stéphane Nicolas stephanenicolas

View GitHub Profile
@stephanenicolas
stephanenicolas / TP.java
Last active Apr 25, 2019
TP for Kotlin & java
View TP.java
import toothpick.Scope
import toothpick.Toothpick
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
annotation class ActivityScope
annotation class FreeScope
//-------------------------
// case: Entry point
@stephanenicolas
stephanenicolas / HeadLineFragment.java
Last active Sep 28, 2017
Invoke the listener method reference on the current activity
View HeadLineFragment.java
public class HeadLineFragment extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int p, long i) {
// Send the event to the host activity
// we should probably check if the activity is finishing|destroyed|rotating
mCallback.onArticleSelected(getActivity(), p);
}
}
@stephanenicolas
stephanenicolas / HeadLineFragment.java
Last active Sep 27, 2017
setHeadlineListener uses Generics to trigger target type inference
View HeadLineFragment.java
public class HeadLineFragment extends Fragment {
public <T> void setHeadlineListener(HeadlineListener<T> listener) {
...
}
}
@stephanenicolas
stephanenicolas / MainActivity.java
Created Sep 26, 2017
Non-Static methods hidden parameters
View MainActivity.java
public static class MainActivity extends Activity {
public void onArticleSelected(int position) {
}
public static void onArticleSelected(MainActivity activity, int position) {
}
}
@stephanenicolas
stephanenicolas / RxChain.java
Created Sep 26, 2017
Using a non static method reference in a Rx chain
View RxChain.java
just(0)
.map(this::onArticleSelected)
.subscribe(...);
@stephanenicolas
stephanenicolas / MainActivity.java
Created Sep 26, 2017
The Lambda Way: setting the listener
View MainActivity.java
public static class MainActivity extends Activity {
public void bindFragment(HeadlinesFragment fragment) {
fragment.setHeadlineListener(MainActivity::onArticleSelected);
}
public void onArticleSelected(int position) {
...
}
}
@stephanenicolas
stephanenicolas / HeadlinesFragment.java
Last active Sep 27, 2017
Lambda Way: HeadlinesFragment
View HeadlinesFragment.java
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 / HeadlineListener.java
Created Sep 26, 2017
Lambda Way: HeadlineListener
View HeadlineListener.java
public interface HeadlineListener<T extends Activity> extends Serializable {
public void onArticleSelected(T activity, int position);
}
@stephanenicolas
stephanenicolas / MainActivity.java
Created Sep 26, 2017
Lambda way: MainActivity
View MainActivity.java
public static class MainActivity extends Activity {
public void onArticleSelected(int position) {
}
}
View HeadlinesFragment.java
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) {