Ejemplo n.º 1
0
  private void initListView(View v) {
    mListView = (RecyclerView) v.findViewById(R.id.list);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    mListView.setLayoutManager(layoutManager);
    mListView.addItemDecoration(
        new SimpleDividerItemDecoration(
            getActivity(), LinearLayoutManager.VERTICAL, 8f, 0xffEDEDED));
    adapter = new FeedAdapter(getActivity(), data, listener);
    mListView.setAdapter(adapter);
    mEmptyView = (EmptyView) v.findViewById(R.id.view_list_empty_layout);
    mEmptyView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {}
        });
    mRefresh = v.findViewById(R.id.refresh);
    mRefresh.setOnClickListener(onClickListener);
    refreshTextView = (TextView) v.findViewById(R.id.refresh_news_count);
    refreshTextView.setOnClickListener(mRefreshNewsListener);
    lineView = v.findViewById(R.id.line);
    swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
            FeedFragment.this.onRefresh();
          }
        });
    mListView.setOnScrollListener(
        new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            // 加载更多
            if (!isLoading
                && adapter.getItemCount() == (layoutManager.findLastVisibleItemPosition() + 1)
                && newState == RecyclerView.SCROLL_STATE_IDLE) {
              isLoading = true;
              adapter.setLoadMoreEnable(true);
              adapter.setLoadMoreState(XListViewFooter.STATE_LOADING);
              adapter.notifyDataSetChanged();
              onLoadMore();
            }
          }

          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            // 解决RecyclerView和SwipeRefreshLayout共用存在的bug
            swipeRefreshLayout.setEnabled(
                layoutManager.findFirstCompletelyVisibleItemPosition() == 0);
          }
        });
    request(Urls.SERVER_PATH + "/user/feeds", 0);
  }
Ejemplo n.º 2
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);
  }