简体   繁体   English

在两个片段中将AsyncTask onPostExecute方法定时在Android中同时完成

[英]Timing AsyncTask onPostExecute method in two Fragments to finish at the same time in android

I have two Android Fragments (Fragment A and Fragment B) both Fragment A and Fragment B executes an AsyncTask that subsequently populates a listView by calling on an API, which works fine. 我有两个Android片段(片段A和片段B),片段A和片段B都执行AsyncTask,随后通过调用API来填充listView,效果很好。 However the listView is Fragment A takes longer to populate than the listView in Fragment B and I need the Fragment B listView and Fragment A listview to be in sync in how long it takes them to completed the AsyncTask onPostExecute method. 但是,listView是Fragment A的填充时间比Fragment B中的listView更长,并且我需要Fragment B listView和Fragment A listview同步它们完成AsyncTask onPostExecute方法所花费的时间。 How do you suggest that I go about accomplishing this. 您如何建议我去实现这一目标。

You can save the results of the Asynctasks after onPostExecute() in variables and call a function to check whether both variable are initialized, and if yes, you can then do your operation of setting the list to the list views. 您可以在onPostExecute()之后将Asynctasks的结果保存在变量中,然后调用一个函数来检查两个变量是否都已初始化,如果是,则可以执行将列表设置为列表视图的操作。

Like, 喜欢,


    List<String> fragAList,fragBList;

    public void saveFirstList(List<String> data){
        fragAList=data;
        updateLists();
    }

    public void saveSecondList(List<String> data){
        fragBList=data;
        updateLists();
    }

    public void updateLists(){
        if(fragAList!=null && fragBList!=null){
            setDataToListViews(fragAList,fragBList);
        }
    }

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

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