I have a RecyclerView in Fragment, item clicks are handled using RxJava2 as explained in this SO answer, It's working fine in non fragments.
private PublishSubject<Place> itemViewClickSubject = PublishSubject.create();
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_result_view, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
// convert click events into reactive stream
RxView.clicks(view)
.takeUntil(RxView.detaches(parent))
// viewHolder.getPlace() is null until it get bound
.map(__ -> viewHolder.getPlace())
.subscribe(itemViewClickSubject);
return viewHolder;
}
When the RecyclerView in the fragment it works fine only for one time. On item click fragment is replaced with another fragment. After back press, RecyclerView fragment appears but clicks doesn't work anymore.
It works if I remove the .takeUntil(RxView.detaches(parent))
line. I think it's required to break RxView click's strong bindings with the the RecyclerView when it is not displayed (Detached). How can I solve the issue? I am looking to solve the issue using one of attach/detach events, any suggestion would be great!. Thanks.
.takeUntil(RxView.detaches(parent))
part. What would be the reason for use of that in the original SO answer. Now I think since it doesn't do any unsubscribe, having that part in the first place makes no sense. Please correct me. – FollyonBindViewHolder
I don't have any reference to parent there. Does parent is always points to parent recycle view? If so I can use that in sidetakeUntil()
. – Folly