本文介绍了延迟UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#FF9494"));
我在OnItemClickListener
中编写的这段代码.
This piece of code I'm writing in OnItemClickListener
.
设置颜色后,我想将该颜色保留4秒钟,然后将该项目的颜色恢复为以前的颜色(例如白色).
After setting the color I want to keep this color for a time of 4 Seconds and then restore the color of the item to its previous(say White).
我尝试让UI线程休眠,但是我知道这不是正确的方法.
I tried putting a sleep on the UI thread, but I know that it is not a correct approach.
有人可以建议我如何实现这一目标吗?
Can anyone suggest me how to achieve this?
推荐答案
parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#FF9494"));
// Start new Thread that sets the color back in 4 seconds
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(4000); // Sleep 4 seconds
// Now change the color back. Needs to be done on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#000000")); // use whatever other color you want here
}
});
}
}).start();
这篇关于延迟UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!