问题
I have Seekbar and I implemented source code as below:
seekProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
adjustCombination(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Because function: adjustCombination(progress) need 0,2s to finish executing so when I move Seekbar, it's not smooth. So how I can fix the issue ?
回答1:
you can perform your background task using AsyncTask like this,
seekProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
new getData().execute(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
now, you have to define getData() to perform and pass the arguments whichever you required. in you case, we have to pass the progress,
private class getData extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... progress) {
// perform operation you want with String "progress"
String value = "hello" + progress;
return value;
}
@Override
protected void onPostExecute(String progressResult) {
// do whatever you want in this thread like
// textview.setText(progressResult)
super.onPostExecute(progressResult);
}
}
so, PreExecute method will be executed before performing any task in background, then your doInBackground method will be called and you will get arguments pass in this method after doInBackground onPostExecute method will be called which will receive the result returned from the doInBackground method. I hope you get it.
回答2:
Don't do it on the UI thread. Make a background thread instead, and handle the callback. Then update your UI on the UI thread if needed.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// your async action
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// update the UI (this is executed on UI thread)
super.onPostExecute(aVoid);
}
}.execute();
回答3:
If it is suited,move your calculation code in onStopTrackingTouch()
method. This way it will only be called once when you stop sliding on the seekbar.
来源:https://stackoverflow.com/questions/51818249/move-seekbar-not-smooth