progressDialog animations pauses on large setText(

2020-07-09 08:27发布

I'm trying to set a large text in a EditText, the operation can take over 30 seconds so I use a ProgressDialog. It shows up, but there's no animation, and then disappears when the operation is done. Here's my simplified code:

class FileOpener extends AsyncTask<File, Integer, String> {

    private ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        progress = new ProgressDialog(context);
        ...
        progress.show();
    }

    @Override
    protected StringBuilder doInBackground(File... files) {
        return readFile();
    }


    @Override
    protected void onPostExecute(String content) {
        EditText editText = ...
        editText.post(new Runnable() {
            @Override
            public void run() {
                editText.setText(content);
                progress.dismiss();
            }
        });
    }
}

What can I do to animate the progress dialog while setting text?

I also tried using this in onPostExecute, same thing, dialog is there but no animation...

EditText editText = ...
new Thread() {
    @Override
    public void run() {
        editor.setText(content);
        progress.dismiss();
    }
}.start();

EDIT - This is NOT a question on my EditText speed, which is awful as I understood. This question is here. No matter how I improve the speed, setting the text will ALWAYS take a few seconds with big files, it doesn't even with top editing apps. What this question is really about is how to keep the loading dialog animation, because currently, it doesn't animate while setting the text in the EditText. I know nothing nothing can be changed on the UI if not in UI thread, then how can I update/animate the loading? If this is impossible or simply too complicated or hacky, then how can I show a loading animation of any sort while setting the text.

1条回答
姐就是有狂的资本
2楼-- · 2020-07-09 08:28

You should be able to minimize the animation pausing/stopping by creating an EditText and setting its text in your doInBackground and adding it to view in your onPostExecute (the ui thread). Here is an example using an Activity and variation of the task you already have with some code comments:

public class Activity extends AppCompatActivity {

    /**
     * dummy edit text (or the old one if doing an update)
     */
    private EditText dummyEditTextForShow;
    /**
     * that dummy's parent
     */
    private ViewGroup viewGroupToAddEditTextTo;
    /**
     * that dummy's position in it's parent
     */
    int indexOfDummy;
    /**
     * your progress dialog
     */
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // create the things
        progress = new ProgressDialog(this);
        dummyEditTextForShow = new EditText(this);
        // show the place holder
        setContentView(dummyEditTextForShow);
        // get its parent and index in parent
        viewGroupToAddEditTextTo = (ViewGroup) dummyEditTextForShow.getParent();
        indexOfDummy = viewGroupToAddEditTextTo.indexOfChild(dummyEditTextForShow);
        new FileOpener().execute();
    }

    class FileOpener extends AsyncTask<File, Integer, EditText> {

        @Override
        protected void onPreExecute() {
            progress.show();
        }
        @Override
        protected EditText doInBackground(File... files) {
            EditText newEditText = new EditText(Activity.this);
            newEditText.setText(readFile());
            return newEditText;
        }

        @Override
        protected void onPostExecute(EditText editText) {
            //get rid of old for the new
            ViewGroup.LayoutParams oldLayoutParams = dummyEditTextForShow.getLayoutParams();
            viewGroupToAddEditTextTo.removeView(dummyEditTextForShow);
            viewGroupToAddEditTextTo.addView(editText, indexOfDummy, oldLayoutParams));
            progress.hide();
            dummyEditTextForShow = editText
        }
    }
}

I tested the above using a http://www.webpagefx.com/tools/lorem-ipsum-generator/loremipsum/paragraphs Paragraphs: 100 Size: 46929 bytes

Set to one string value fiftykb and then created another String all very lazily and set it to the new edit text (instead of your readFile()):

String all = fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb;
newEditText.setText(all)

Results were that the animation never stops, but there is a brief moment (< 1 sec) the edit text is blank and then shows all the text and is editable.

HTHs!

查看更多
登录 后发表回答