2016-03-04 77 views
1

我遇到了一个问题,当我尝试在刷新时使用滑动刷新布局运行异步任务时,它“冻结”并且不旋转。任务完成后,它会消失。执行异步任务时刷卡冻结

这里是我的代码:

HotActivityFragment.java:

public class HotActivityFragment extends Fragment { 

    ListView hotList; 
    SwipeRefreshLayout mSwipeRefreshLayout; 
    Context context; 
    SharedPreferences sharedPreferences; 
    HotListAdapter hotListAdapter; 
    public HotActivityFragment() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_hot, container, false); 
     context = getContext(); 
     mSwipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.activity_main_swipe_refresh_layout); 
     hotList = (ListView)view.findViewById(R.id.hotListView); 
     hotList.setOnScrollListener(new EndlessScrollListener(getActivity())); 
     sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE); 
     try { 
      ArrayList<ListTypeItem> initial_list = new DownloadPosts(getActivity()).execute().get(); 
      this.hotListAdapter = new HotListAdapter(getContext(), initial_list); 
      hotList.setAdapter(hotListAdapter); 
     }catch(Exception e) 
     { 
      Log.d("Download Error", e.toString()); 
     } 
     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       retrievePosts(); 
      } 

     }); 
     mSwipeRefreshLayout.setColorSchemeResources(R.color.accentColor, R.color.backgroundColor); 



     return view; 
    } 
    public void retrievePosts() 
    { 
     // showing refresh animation before making http call 
     mSwipeRefreshLayout.setRefreshing(true); 

     //shared preferences = empty 
     sharedPreferences.edit().putString("last_time_downloaded", "empty").commit(); 
     try { 
      ArrayList<ListTypeItem> listItems = new DownloadPosts(getActivity(), mSwipeRefreshLayout).execute().get(); 
      hotListAdapter.updateList(listItems); 
      hotListAdapter.notifyDataSetChanged(); 
     } catch (Exception e) { 
      Log.d("Download Error", e.toString()); 
     } 

     mSwipeRefreshLayout.setRefreshing(false); 

     //for testing purposes 
//  new Handler().postDelayed(new Runnable() { 
//   @Override public void run() { 
//    mSwipeRefreshLayout.setRefreshing(false); 
//   } 
//  }, 5000); 
    } 
    } 

DownloadPosts.java:

public class DownloadPosts extends AsyncTask<Void, Void, ArrayList<ListTypeItem>> { 

    SharedPreferences sharedPreferences; 
    SwipeRefreshLayout swipeRefreshLayout; 

    public DownloadPosts(Activity activity) 
    { 
     this.sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE); 
    } 
    public DownloadPosts(Activity activity, SwipeRefreshLayout swipeRefreshLayout) 
    { 
     this.sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE); 
     this.swipeRefreshLayout = swipeRefreshLayout; 
    } 
    @Override 
    protected ArrayList<ListTypeItem> doInBackground(Void... args) 
    { 
     StringBuilder parsedString = new StringBuilder(); 

     ArrayList<ListTypeItem> downloadList = new ArrayList<>(); 
     StringBuilder str = new StringBuilder(); 

     if(sharedPreferences.getBoolean("Thomas More",false)) 
     { 
      str.append("190155257998823,"); 
     } 

     String school_url = str.toString(); 
     if(school_url.length() > 0) 
     { 
      school_url = school_url.substring(0, str.length()-1); 
     } 
     try{ 
      String date = ""; 
      //checken of opnieuw moet bepaald worden 
      // + in de adapter moet als gereload wordt last_time_downloaded == empty 
      if(!sharedPreferences.getString("last_time_downloaded","empty").equals("empty")) 
      { 
       String last_date = sharedPreferences.getString("last_time_downloaded","nothing"); 
       last_date = last_date.replace(" ","T"); 
       date= "&datum_last_posted=" + last_date; 
      } 
      URL url = new URL("http://localhost/getpostlist.php?school_post=" + school_url + date); 
      URLConnection conn = url.openConnection(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String json; 
      while((json = bufferedReader.readLine())!= null) 
      { 
       parsedString.append(json + "/n"); 
      } 
      String s = parsedString.toString().trim(); 
      //converten van string opgehaald via http naar jsonobject 
      JSONArray array = new JSONArray(s); 
      for(int i = 0; i < array.length(); i++) 
      { 
       JSONObject tempObj = array.getJSONObject(i); 
       School_WithoutImage tempSchool = new School_WithoutImage(tempObj.getString("school_id"), 
         tempObj.getString("post_message"),tempObj.getInt("views"),tempObj.getInt("likes") 
         ,tempObj.getInt("post_id"),tempObj.getString("datum_posted")); 
       downloadList.add(tempSchool); 
       if(i == array.length()-1) { 
        sharedPreferences.edit().putString("last_time_downloaded",tempObj.getString("datum_posted")).commit(); 
       } 
      } 
      JSONObject obj = array.getJSONObject(0); 
     }catch(Exception e) 
     { 
      Log.d("Exception", e.toString()); 
     } 

     return downloadList; 
    } 
    @Override 
    protected void onPostExecute(ArrayList<ListTypeItem> result) 
    { 
     if(this.swipeRefreshLayout != null) 
     { 
//   swipeRefreshLayout.setRefreshing(false); 
     } 
    } 

} 

我不知道为什么swiperefreshview不旋转。任何人有想法?

回答

2

因为调用get()

.execute().get() 

强制UI线程等待AsyncTask完成。

相反,你应该看看在onPostExecute方法这样做:

protected void onPostExecute(ArrayList<ListTypeItem> listItems) { 
     hotListAdapter.updateList(listItems); 
     hotListAdapter.notifyDataSetChanged(); 
} 
+0

我试图把它放在onPostExecute之前,但完全忘了删除。获得(),感谢您的回答! – Diederik

0

因为您正在等待asynctask的结果,只需在执行后调用get即可。并进一步传递给列表。

您可以使用本地广播听众或可以创建一个接口,可我们回调,不结冰UI