Example #1
0
  /** @param type 0 refresh 1 more */
  private void request(String url, final int type) {
    if (type == 0) {
      stopRefreshNews();
      showRefreshNewsCount(null, false);
      refreshing = true;
      mHandler.removeCallbacks(stopRefreshRunnable);
    }
    if (adapter.getItemCount() == 0) {
      mEmptyView.show(true);
      mEmptyView.onLoading();
    }
    mEmptyView.setOnClickListener(null);
    GsonRequest<FeedDatasGsonModel> req =
        new GsonRequest<FeedDatasGsonModel>(
            url,
            FeedDatasGsonModel.class,
            AppUtils.getOAuthMap(getActivity()),
            new Response.Listener<FeedDatasGsonModel>() {
              @Override
              public void onResponse(FeedDatasGsonModel response) {
                if (response != null) {
                  nextUrl = response.next;
                  if (response.data != null && !response.data.isEmpty()) {
                    if (type == 0) {
                      data.clear();
                    }
                    data.addAll(response.data);
                  }
                }

                if (!TextUtils.isEmpty(nextUrl)) {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(0);
                } else {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(XListViewFooter.STATE_NOTDATA);
                }

                adapter.notifyDataSetChanged();
                if (type == 0) {
                  offset = response != null ? response.offset : null;
                  if (!TextUtils.isEmpty(offset)) {
                    try {
                      offset = URLEncoder.encode(offset, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                      e.printStackTrace();
                    }
                    startRefreshNews();
                  }
                  mRefresh.clearAnimation();
                  refreshing = false;
                  swipeRefreshLayout.setRefreshing(false);
                } else isLoading = false;
                if (adapter.getItemCount() <= 0) {
                  mEmptyView.show(true);
                  mEmptyView.onFailed(getString(R.string.no_data));
                } else if (mEmptyView.isShowing()) mEmptyView.show(false);
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                if (mRefresh != null) mRefresh.clearAnimation();
                if (type == 0) {
                  if (!TextUtils.isEmpty(offset)) {
                    startRefreshNews();
                  }
                  if (data.isEmpty()) {
                    mEmptyView.show(true);
                    ErrorEntity entity = AppUtils.getErrorEntity(error);
                    mEmptyView.onFailed(
                        entity != null && !TextUtils.isEmpty(entity.getMessage())
                            ? entity.getMessage()
                            : getString(R.string.chat_load_failed));
                    mEmptyView.setOnClickListener(mRefreshNewsListener);
                  }
                  refreshing = false;
                  swipeRefreshLayout.setRefreshing(false);
                } else {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(0);
                  adapter.notifyDataSetChanged();
                  isLoading = false;
                }
              }
            });
    req.setShouldCache(false);
    addRequest(req);
  }
Example #2
0
 @Override
 public void run() {
   showRefreshNewsCount(null, false);
 }