How to handle Item clicks for a recycler view using RxJava

后端 未结 5 692
有刺的猬
有刺的猬 2021-02-01 21:49

I was interested to find out what is the best way to respond to a item click of a recycler view.

Normally I would add a onclick() listener to the ViewHolder and pass ba

5条回答
  •  日久生厌
    2021-02-01 22:43

    Step 1: Move the business logic out of the activities to domain classes/services

    Optional: Use https://github.com/roboguice/roboguice to easily wire up your services with each other.

    Step 2: @Inject (or just set) your service into your adapter

    Step 3: Grab https://github.com/JakeWharton/RxBinding and use the super powers in your adapter:

    RxView.clicks(button).subscribe(new Action1() {
        @Override
        public void call(Void aVoid) {
            myCoolService.doStuff();
        }
    });
    

    Step 4: Get a runtime crash and learn how to deal with subscriptions

    Step 5: PROFIT :)

提交回复
热议问题