private PublishSubject<View> clickedView = PublishSubject.create();
public Observable<View> onClickView() {
return clickedView;
}@Override
public SettingsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell_settings, parent, false);
SettingsViewHolder vh = new SettingsViewHolder(v);
ViewObservable.bindView(parent, ViewObservable.clicks(v))
.map(new Func1<OnClickEvent, View>() {
@Override
public View call(OnClickEvent onClickEvent) {
return onClickEvent.view();
}
})
.subscribe(clickedView);
return vh;
}ViewObservable.bindView(this, _adapter.onClickView())
.subscribe(new Action1<View>() {
@Override
public void call(View view) {
int pos = _recyclerView.getChildPosition(view);
MyItem item = _adapter.itemAt(pos);
Log.e(getClass().getName(), "clicked menu item: " + item.getTitle());
}
});private PublishSubject<View> clickSubject = PublishSubject.create();
public Observable<View> getItemClickSignal() {
return clickSubject;
}
@Override
public InstallationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_installation, parent, false);
RxView.clickEvents(view)
.takeUntil(RxView.detaches(parent))
.map(new Func1<ViewClickEvent, View>() {
@Override
public View call(ViewClickEvent viewClickEvent) {
return viewClickEvent.view();
}
})
.subscribe(clickSubject);
return new InstallationViewHolder(view);
}@Override
public EntryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_day, parent, false);
EntryHolder holder = new EntryHolder(view);
onViewCreated(view, parent);
return holder;
}
public abstract void onViewCreated(View view, ViewGroup parent);
The owning Fragment/Activity then creates a new Adapter and implements onViewCreated - below example is based on a Fragment:
public class SomeFragment extends Fragment {
private Subscription recyclerViewSubscription;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adapter = new EntryAdapter() {
@Override
public void onViewCreated(final View view, ViewGroup parent) {
recyclerViewSubscription = RxView.clicks(view)
.takeUntil(RxView.detaches(parent))
.map(new Func1<Void, View>() {
@Override
public View call(Void aVoid) {
return view;
}
})
.subscribe(new Action1<View>() {
@Override
public void call(View view) {
I just started using RxJava today - so like I said: Might not be very elegant and I am looking for a better solution.
int position = recyclerView.getChildAdapterPosition(view);
Log.i(TAG, "User clicked on item at position " + position);
}
});
}
};
//Set the adapter on the recyclerview...
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (recyclerViewSubscription != null) {
recyclerViewSubscription.unsubscribe();
}
recyclerView.setAdapter(null);
}
}
P.s. i guess one way to get rid of that onViewCreatedCallback would be, if the recycler view posts to an event bus once the view has been created and the fragment creates the subscribe to onclick in the method that gets notified by the event bus. But iam guessing there is a more straight forward way involving only rxjava.
private PublishSubject<View> clickSubject = PublishSubject.create();
public Observable<View> getItemClickSignal() {
return clickSubject;
}
@Override
public InstallationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_installation, parent, false);
RxView.clickEvents(view)
.takeUntil(RxView.detaches(parent))
.map(new Func1<Void, View>() {
@Override
public View call(Void empty) {
return view;