RecyclerView item click using RxJava2 + RxBinding

2019-07-31 18:24发布

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.

1条回答
Deceive 欺骗
2楼-- · 2019-07-31 18:56

To answer your question and comments I send my entire solution for RecylerView + RxJava:

private PublishSubject<Place> itemViewClickSubject = PublishSubject.create();

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    RxView.clicks(viewHolder.itemView) //viewHolder.itemView here You have access to view
            .map(aVoid -> viewHolder.getPlace())
            .subscribe(itemViewClickSubject);
}

@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    itemViewClickSubject.onComplete(); //here we avoid memory leaks
}
查看更多
登录 后发表回答