RecyclerView item click using RxJava2 + RxBinding not working after Fragment replacements

房东的猫 提交于 2019-12-24 09:36:02

问题


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:


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
}


来源:https://stackoverflow.com/questions/44433115/recyclerview-item-click-using-rxjava2-rxbinding-not-working-after-fragment-rep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!