@Override public void onRefresh() { if (Application.connectedType == ConnectivityManager.TYPE_WIFI) { String url; if (cacheList.size() == 0) { // 如果不判断,那么在第一次进入断网未加载出数据,再次刷新时会奔溃 url = Interface.fuliImageUrl + "&page=0&page_size=30&max_timestamp=-1&latest_viewed_ts=0"; } else { url = Interface.fuliImageUrl + "&page=0&page_size=30&max_timestamp=-1&latest_viewed_ts=" + cacheList.get(0).getUpdate_time(); } HttpUtils.get(url, new GetData(false)); } else if (Application.connectedType == ConnectivityManager.TYPE_MOBILE) { ViewUtils.showToast(getString(R.string.not_allow_refresh)); mRefreshLayout.setRefreshing(false); } else { ViewUtils.showToast(getString(R.string.not_connect_netWork)); mRefreshLayout.setRefreshing(false); } }
@Override public void success(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray jsonArray = (JSONArray) jsonObject.opt("items"); List<FuLiImageBean> beautyImageList = JsonUtils.jsonArray2list(jsonArray.toString(), FuLiImageBean.class); if (isGetDataByScroll && beautyImageList != null) { // 当前是滚动获取数据 for (int i = 0; i < beautyImageList.size(); i++) { cacheList.add(beautyImageList.get(i)); } adapter.notifyDataSetChanged(); adapter.removeFooterView(); page++; } else if (beautyImageList != null) { // 第一次进入或者正在刷新 List<ImageCache> mCacheData = DBUtils.queryImageCache(urlType); if (mCacheData.size() == 0) { mRefreshLayout.setRefreshing(false); // 第一次进入 for (int i = 0; i < beautyImageList.size(); i++) { cacheList.add(beautyImageList.get(i)); } adapter = new FuLiAdapter(cacheList, R.layout.item_recyclerview_beautyimage); mRecyclerView.setAdapter(adapter); adapterListener(adapter); DBUtils.addImageCache(jsonArray.toString(), "", urlType); page++; } else { // 正在刷新 mRefreshLayout.setRefreshing(false); int newDataTime = Integer.parseInt(beautyImageList.get(0).getUpdate_time()); int oldDataTime = Integer.parseInt(cacheList.get(0).getUpdate_time()); // 如果获取到的第一条数据和缓存List中的第一条数据相同,则没有新数据 if (newDataTime == oldDataTime) { ViewUtils.showToast(getString(R.string.NoMoreNewData)); } else { // 900秒更新一条数据,哈哈,这都被我发现了 int newDataNum = (newDataTime - oldDataTime) / 900; ViewUtils.showToast( getString(R.string.update_new_data_num).replace("***", newDataNum + "")); if (newDataNum > 30) { cacheList.clear(); } else { beautyImageList = beautyImageList.subList(0, newDataNum); } for (int i = 0; i < beautyImageList.size(); i++) { cacheList.add(i, beautyImageList.get(i)); } adapter.notifyDataSetChanged(); DBUtils.clearImageCache(urlType); DBUtils.addImageCache(jsonArray.toString(), "", urlType); } } } isLoading = false; } catch (JSONException e) { isLoading = false; mRefreshLayout.setRefreshing(false); adapter.removeFooterView(); e.printStackTrace(); } }