Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调用 setTickCount 时机不对导致setProgress 无效的问题 #25

Open
FhrShadow opened this issue Nov 21, 2020 · 0 comments
Open

Comments

@FhrShadow
Copy link

由于想要用一套layout来复用,想要在代码离根据不同的数据设置不同的tickcount和max等参数,然后发现setProgress无效,出问题的代码是这样:

preference.seekBar.tickCount = 4
preference.seekBar.min = 0.toFloat()
preference.seekBar.max = 3.toFloat()
preference.seekBar.setProgress(1.0f)

经过检查发现是setProgress时会在一个数组mProgressArr里找最接近参数传进来的值,这个mProgressArr里的值本应该是seekbar中每个刻度(tick)的对应的值,换言之在本例子应该为[0.0, 1.0, 2.0, 3.0]

但是mProgressArr此时里面的值为[0.0, 33.333, 66.666, 100]

进一步排查发现在setTickCount的时候才会重新计算这个mProgressArr里的值,关键在于下面这段代码

mProgressArr = new float[mTicksCount];
for (int i = 0; i < mProgressArr.length; i++) {
     mProgressArr[i] = mMin + i * (mMax - mMin) / ((mTicksCount - 1) > 0 ? (mTicksCount - 1) : 1);
}

可以看到时通过minmax来计算出每个 tick 对应的值的,所以用例代码无法工作的理由在于应该先调用setMinsetMax再调用setTickCount

建议setMinsetMax方法也要重新计算这个mProgressArr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant