问题
I have array with more elements than posted here. I show only 2 in post but there are 10 showGreen
variables, 10 additional threads, 10 "TextViews" and 10 isGreenBackgroundShouldAppear
functions. As you see, when answer is correct, TextView1
or TextView2
becomes green on ACTION_DOWN
and sets random image in image_view
thus preparing next question. The problem is that if I use 10 threads which separately set value of answer for every TextView
every 500ms, if user press correct answer TextView
too fast, it will be treated as wrong answer (red). If I set from 500ms to 300ms or less, sometimes showGreen
never gets updated correctly. Do you maybe know any solution for my problem?
class Cats : AppCompatActivity() {
var cats = intArrayOf(
R.drawable.cat1,
R.drawable.cat2,
R.drawable.cat3,
R.drawable.cat4,
R.drawable.cat5,
R.drawable.cat6,
R.drawable.cat7,
R.drawable.cat8,
R.drawable.cat9,
R.drawable.cat10,
R.drawable.cat11,
R.drawable.cat12,
R.drawable.cat13,
R.drawable.cat14,
R.drawable.cat15,
R.drawable.cat16,
R.drawable.cat17
)
lateinit var random: Random
var showGreen1 = false
var showGreen2 = false
val runnable1 = Runnable {
while (true) {
showGreen1 = isGreenBackgroundShouldAppear1()
Thread.sleep(500)
}
}
val runnable2 = Runnable {
while (true) {
showGreen2 = isGreenBackgroundShouldAppear2()
Thread.sleep(500)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
random = Random()
Thread(runnable1).start()
Thread(runnable2).start()
TextView1.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
if (showGreen1) {
TextView1.setBackgroundResource(R.color.green);
image_view.setImageResource(cats[random.nextInt(cats.size)])
/*showGreen1 = isGreenBackgroundShouldAppear1()
showGreen2 = isGreenBackgroundShouldAppear2()*/
} else {
TextView1.setBackgroundResource(R.color.red);
}
}
MotionEvent.ACTION_UP -> {
TextView1.setBackgroundResource(R.color.white)
}
}
return@OnTouchListener true
})
TextView2.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
if (showGreen2) {
TextView2.setBackgroundResource(R.color.green);
image_view.setImageResource(cats[random.nextInt(cats.size)])
} else {
TextView2.setBackgroundResource(R.color.red);
}
}
MotionEvent.ACTION_UP -> {
TextView2.setBackgroundResource(R.color.white)
}
}
return@OnTouchListener true
})
/*while(true)
{
showGreen1 = isGreenBackgroundShouldAppear1()
showGreen2 = isGreenBackgroundShouldAppear2()
}*/
fun isGreenBackgroundShouldAppear1(): Boolean {
return image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat1
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat2
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat3
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat4
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat5
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat6
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat7
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat8
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat9
)?.constantState
}
fun isGreenBackgroundShouldAppear2(): Boolean {
return image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat10
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat11
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat12
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat13
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat14
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat15
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat16
)?.constantState ||
image_view.drawable.constantState == ContextCompat.getDrawable(
this,
R.drawable.cat17
)?.constantState
}
}
来源:https://stackoverflow.com/questions/59523159/how-to-solve-latency-issue-in-quiz-application