简体   繁体   中英

multiple dependent seekbars android

Let say I have 4 seekbars and there values are initially set to 25 each. If I change the value of any one seekbar, then this change should reflect on the other 3 seekbars as well, like if I increase the value then all the other seekbars should decrease and vice versa..

I know this requires a mathematical formula which I am unable to figure out... Thanks in advance

It sounds like you want the four SeekBars to total 100 and that when one increases, the other three should decrease proportionally?

It sounds like you need to implement SeekBar.OnSeekBarChangeListener http://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html

You'll want your activity to have references to all four, and you want the same listener (which could be your Activity if you have it implement the above mentioned interface) and set this listener on all of your SeekBars. On OnSeekBarChangeListener.onStartTrackingTouch you'll want to record which SeekBar is being modified and what the value of all four SeekBars is at the time. In OnSeekBarChangedListener.onProgressUpdate, take the change from the original value as a percentage: (new value - original value) / original value. Divide that by three (to distribute the change among the remaining three SeekBars) and then apply this to the values of the others. Should be something like: original value * (1 - (change of first SeekBar / 3))

My math could have a mistake in it, but that's the general idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM