Search code examples
androidandroid-layoutlistviewandroid-viewandroid-adapterview

ListView: change siblings' data dynamically


I have a ListView with items containing each one a SeekBar.

What I want to achieve is that when one of the SeekBars is slowly slided, the rest of the SeekBars gracefully slide at the same time (this is, I want the events of one items to make effect on the rest of the list items).

I tried to store all the views in an array of views inside the adapter, but I was getting unexpected and unpredictable behavior (I guess it is related to the way ListView manages the child items internally).

Any hint on how this should be properly done?

Thank you very much!


Solution

  • I would solve the problem using RxJava.

    Basically, you have a stream of data (Seekbar's current position) and have arbitrary amount of subscribers (each Seekbar of each row).

    You need some kind of Observable, that possesses following features:

    1. you can set a value to that Observable
    2. upon subscription you are able to receive the value, that was previously dispatched

    Turns out BehaviorSubject is the one that you need.

    So, having declared a BehaviorSubject this was:

    BehaviorSubject<Integer> subject = BehaviorSubject.createDefault(0); // default value is 0
    

    Now anytime any Seekbar's value is changed just perform subject.onNext(currentPosition).

    As soon as the view is being shown on the screen - subscribe to the stream:

    Disposable d = subject.subscribe(new Consumer<Integer>() {
        @Override
        public void accept(Integer integer) throws Exception {
            seekbar.setProgress(integer);
        }
    )
    

    When the view is being recycled - dispose from the Disposable:

    d.dispose();