其他分享
首页 > 其他分享> > android-用于异步任务的ProgressBar微调框不起作用

android-用于异步任务的ProgressBar微调框不起作用

作者:互联网

我的问题是,当我单击按钮“单击”以调用我的API时,应该在我调用API时显示我的progressBar(spinner).相反,我的应用程序冻结了不到一秒钟,然后在调用完成后显示了我的加载微调器一小段时间(它只是闪烁)

这是我的代码

private ProgressBar spinner;
public View onCreateView(...)
{
    spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
    spinner.setVisibility(View.GONE);
    ...
}



private class MyTask extends AsyncTask<String, Void, Void> {

    String pageContent = "";
    DataOutputStream wr;


    @Override
    protected void onPreExecute() {
        spinner.setVisibility(View.VISIBLE);
    }

    @Override
    public Void doInBackground(String... params) {

        requestResult.setSuccess(false);
        HttpURLConnection connection;

        try {
            String url = "myURL";

            //I only call my Api here. I delete rest so this won't bother you

            requestResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        JsonParser parser = new JsonParser();
        //Same here i delete nonimportant 

        MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
        done();
        spinner.setVisibility(View.GONE);
    }

}

我尝试过各种方法,它们总是冻结一秒钟,然后加载微调器闪烁,并显示API中的内容.

你能帮我吗?

我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view_send"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/buttonSend"
            android:visibility="gone"/>
    </RelativeLayout>
</ScrollView>

解决方法:

请尝试使用此工作示例,将微调框传递给MyTask,如下所示:

private class MyTask extends AsyncTask<String, Void, Void> {

    String pageContent = "";
    DataOutputStream wr;

    private final ProgressBar progress;

    public MyTask(final ProgressBar progress) {
        this.progress = progress;
    }


    @Override
    protected void onPreExecute() {
        progress.setVisibility(View.VISIBLE);
    }

    ...

    @Override
    protected void onPostExecute(Void result) {
        JsonParser parser = new JsonParser();
        //Same here i delete nonimportant 

        MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
        done();
        progress.setVisibility(View.GONE);
    }
}

然后调用new MyTask(progress).execute();

编辑:在您的问题中,您指的是在调用MyTask时手机处于冻结状态.
请检查此步骤,以避免在AsyncTask中冻结UI:

>不要使用新的MyTask().get()调用MyTask
>尝试移至doInBackground这部分MyResponse = new Gson().fromJson(jsonContent,MyResponse.class); done();这可能很昂贵.

希望能有所帮助!

标签:android-asynctask,android-progressbar,android
来源: https://codeday.me/bug/20191118/2032179.html