Skip to content Skip to sidebar Skip to footer

Move Seekbar Not Smooth

I have Seekbar and I implemented source code as below: seekProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void

Solution 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.


Solution 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();

Solution 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.


Post a Comment for "Move Seekbar Not Smooth"