简体   繁体   English

处理网页时显示加载图标

[英]show loading icon while processing a web page

My MainActivity calls HandleWebPage.init() to download and parse a web page link. 我的MainActivity调用HandleWebPage.init()来下载和解析网页链接。 After init() call, I will get set of strings (3 strings) . 在init()调用之后,我将获得一组字符串(3个字符串)。 Each string is shown in a separate fragment (default first fragment with first string). 每个字符串都显示在单独的片段中(默认的第一个片段带有第一个字符串)。 Other fragments can be selected via navigation drawer. 其他片段可以通过导航抽屉选择。

Issue is I am trying to add progress dialog which should show loading icon (rotating circle) till Handle Webpage.init() gets completed. 问题是我试图添加进度对话框,该对话框应显示加载图标(旋转的圆圈),直到Handle Webpage.init()完成。

init() function internally calls 2 async tasks ; init()函数在内部调用2个异步任务; One to download the web page and second is to process the web page using jsoup to get above strings. 一种下载网页,第二种是使用jsoup处理网页以获取上述字符串。

How can I add progress dialog to while init() function getting processed and returned to MainActivity? 我如何在处理init()函数并返回MainActivity的同时向其添加进度对话框?

I have tried adding init() in AsyncTask and putting progress dialog in onPreExecution etc.., did not work. 我试过在AsyncTask中添加init()并将进度对话框放在onPreExecution等中.. Also tried to create progressdialog.show() then create a thread and started and called init() did not work. 还尝试创建progressdialog.show()然后创建线程并启动并调用init()不起作用。 I would like to know what would be right solution Below is code snippet. 我想知道什么是正确的解决方案下面是代码片段。

MainActivity.java /// ------------snip
if(savedInstanceState == null) {
            updateYearMonthDay();  //gets year, month, day
            //hwp is instance of HandleWebPage class
            // hwp.init() internally downloads a webpage and process
            // This takes some time and I require to show some progress around this
            hwp.init(year, month, day);  
            // after complete i get 3 strings which is processed further in below function
            updateMetaData();
            // this show first fragment with fist string in text view.
            SelectItem(0);

        }

HandleWebPage.java
--------------------
public void init()
{
           doc = downloadPage();
           if (doc != null) {
                  extractlinks(doc);
           }
// some other function which uses AsyncTask as 
String = new getmoreInfo().execute(dataString1).get();
}

public Document downloadPage()
{
try {
        doc = new getWebPage().execute(urlString).get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return doc;       
}

public class getWebPage extends AsyncTask<String, Document , Document> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Document doInBackground(String... params) {
        try {
            doc = Jsoup.connect(params[0]).timeout(0).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(doc == null) {
            return null;
        } else
            return doc;
    }

    @Override
    protected void onPostExecute(Document doc1) {
        super.onPostExecute(doc1);
    }
};

public void extractLinks(Document doc)
{
// code to process doc using jsoup and update respective String in this class.
}

You can probably merge the two AsyncTask s that you perform sequentially into a single AsyncTask. 您可以将顺序执行的两个AsyncTask合并到一个AsyncTask中。 For showing a progressbar during the AsyncTask , initialize the progressbar with a visibility of GONE when the activity is created. 为了在AsyncTask期间显示进度条,请在创建活动时以GONE可见性初始化进度条。 Then set the visibility to VISIBLE in the onPreExecute() method of AsyncTask and set it back to GONE in onPostExecute() method. 然后将可见性设置为VISIBLEonPreExecute()的方法AsyncTask和设置回GONEonPostExecute()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM