SeekBar setProgress破坏了二级进度 [英] SeekBar setProgress trashes secondary progress
问题描述
我的主要和次要进度同时增长,次要进度的增长速度快于主要进度. 每次更新主要进度时,次要进度都会丢失(就像零或小于主要进度).这会产生令人讨厌的闪烁.
I have primary and secondary progress growing in the same time, secondary progress grows faster than primary. Everytime I update the primary progress, the secondary one is lost (like it would be zero or less than primary). This produces a nasty flickering.
我发现的唯一解决方法是在设置主数据库后将其设置为辅助数据库.我添加了"+1",因为SeekBar会进行检查,并且如果我设置相同的值也不会重绘.
The only workaround I found is set the secondary to itself after setting the primary. I added "+1" as SeekBar has a check and doesn't redraw if I set the same value.
mSeekBar.setProgress(newPosition);
mSeekBar.setSecondaryProgress(mSeekBar.getSecondaryProgress()+1); // WTF?
第二行可能会解决它,但它看起来很荒谬,我想效率也很低.
The second line may fix it, but it looks ridiculous and I guess inefficient too.
这发生在棒棒糖(5.0.1)-API 21上. 它以前曾经可以正常工作-无法找出原因.
This happens on Lollipop (5.0.1) - API 21. It used to work fine before - can't figure out why.
如果我为API级别19构建它,则可以正常工作. (视频: API19Seekbar )
If I build for API level 19 it works fine. (video: API19Seekbar)
targetSdkVersion 19
compile 'com.android.support:appcompat-v7:19.+'
如果我针对API级别21进行构建,则会闪烁. (视频: API21SeekBar )
If I build for API level 21 it flickers. (video: API21SeekBar)
targetSdkVersion 21
compile 'com.android.support:appcompat-v7:21.+'
完整的示例活动代码如下:
Full example Activity code follows:
int delay1 = 1000;
int delay2 = 200;
int primaryProgress;
int secondaryProgress;
boolean mStarted;
Button mButton;
SeekBar mSeekBar;
Handler h1 = new Handler();
Handler h2 = new Handler();
Runnable primary = new Runnable() {
@Override
public void run() {
primaryProgress = (primaryProgress + 1) % 100;
mSeekBar.setProgress(primaryProgress);
h1.postDelayed(primary, delay1);
}
};
Runnable secondary = new Runnable() {
@Override
public void run() {
secondaryProgress = (secondaryProgress + 1) % 100;
mSeekBar.setSecondaryProgress(secondaryProgress);
h2.postDelayed(secondary, delay2);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonClick();
}
});
mSeekBar = (SeekBar)findViewById(R.id.seekBar);
}
private void buttonClick() {
mStarted = !mStarted;
if (mStarted) {
mButton.setText("Started");
h1.postDelayed(primary, delay1);
h2.postDelayed(secondary, delay2);
} else {
mButton.setText("Stopped");
h1.removeCallbacks(primary);
h2.removeCallbacks(secondary);
}
}
推荐答案
此问题已在下一个Android版本中修复,但目前最好的解决方法可能是从SDK复制scrubber_progress_horizontal_material.xml
及其9补丁PNG.资源(<sdk-root>/platforms/android-21/data/res/...
),然后重新排列标签,使<layer-list>
位于根目录.
This has been fixed for the next Android release, but the best workaround for now would probably be to copy scrubber_progress_horizontal_material.xml
and its 9-patch PNGs from the SDK resources (<sdk-root>/platforms/android-21/data/res/...
), then to rearrange the tags so that <layer-list>
is at the root.
这是经过修改的XML的外观:
Here is what the revised XML should look like:
drawable-v21/scrubber_progress_horizontal_material_fixed.xml
drawable-v21/scrubber_progress_horizontal_material_fixed.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<nine-patch
android:src="@drawable/scrubber_track_mtrl_alpha"
android:tint="?android:attr/colorControlNormal" />
</item>
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@android:color/transparent" />
</item>
<item>
<nine-patch
android:src="@drawable/scrubber_primary_mtrl_alpha"
android:tint="?android:attr/colorControlNormal" />
</item>
</selector>
</scale>
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@android:color/transparent" />
</item>
<item>
<nine-patch
android:src="@drawable/scrubber_primary_mtrl_alpha"
android:tint="?android:attr/colorControlActivated" />
</item>
</selector>
</scale>
</item>
</layer-list>
,然后在values-v21/styles.xml
和/或布局XML中进行适当的更改,以便默认情况下在API 21+上使用此背景.如果需要,我可以提供这些示例.
And then the appropriate changes in values-v21/styles.xml
and/or your layout XML so that you use this background by default on API 21+. I can include examples for those if you need them.
这篇关于SeekBar setProgress破坏了二级进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!