private void handleMsgPost() { if (Utils.isListEmpty(msgPosts)) return; // Collections.sort(msgPosts, mComparator); nextPage++; mMsgAdapter.appendPostsToTail(msgPosts); showMoreCachedResultsFromAdapter(); // the server may return less than required item // if (msgPosts.size() < Constants.MESSAGE_REQUEST_NUM_FROM_INTERNET) isNoMorePosts = true; }
@SuppressWarnings("unchecked") @Override public void networkCallback(Object jsonResult, int httpCode, int requestTag) { if (mActivity == null) return; JsonData<ListJsonData> jResult = (JsonData<ListJsonData>) jsonResult; if (httpCode == HttpStatus.SC_FORBIDDEN) { ((AppApplication) mActivity.getApplication()).userNotLogin(); return; } if (httpCode != HttpStatus.SC_OK) { Log.e("Volley", "HTTP Code " + httpCode + " catched in callback!!"); String toastString; if (httpCode == HttpStatus.SC_CONFLICT) { toastString = mActivity.getString(R.string.parse_data_failed); } else if (httpCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { toastString = getString(R.string.error_occur_retry_later); } else toastString = mActivity.getString(R.string.connect_to_server_failed); ToastTools.toastWithFrequencyLimited(mActivity, toastString, Toast.LENGTH_SHORT, true); return; } Log.d("Volley", "JsonData apiVersion: " + jResult.getApiVersion()); if (jResult.getCode() != Constants.CODE_SUCCESS) { Log.e("Volley", jResult.getMessage() == null ? "" : jResult.getMessage()); ToastTools.toastWithFrequencyLimited( mActivity, jResult.getMessage(), Toast.LENGTH_SHORT, true); return; } if (jResult.getData().size() == 0) { isNoMorePosts = true; // ToastTools.toast(getActivity(), getString(R.string.no_more_message_notice), // Toast.LENGTH_SHORT).show(); return; } if (requestTag == REQUEST_LATEST_COMMENTED_AND_LIKES) { List<PostMessage> msgs = jResult.getData().getPostMsgList(); mMsgAdapter.clearAllComments(); if (!Utils.isListEmpty(msgs)) msgPosts.addAll(msgs); handleMsgPost(); } else if (requestTag == REQUEST_PREVIOUS_COMMENTED_AND_LIKES) { List<PostMessage> msgs = jResult.getData().getPostMsgList(); if (!Utils.isListEmpty(msgs)) msgPosts.addAll(msgs); handleMsgPost(); } }
private void showMoreCachedResultsFromAdapter() { boolean isMoreCachedCanBeShown = mMsgAdapter.showMoreCachedResult(); // already notified data changes if (!isMoreCachedCanBeShown) { if (isNoMorePosts) { if (!isFooterShow) { mMsgListView.addFooterView(footerView); isFooterShow = true; } } else { getPreviousPostsFromInternet(); } } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_msg_comment, null); mMsgListView = (ListView) view.findViewById(R.id.msg_comment_list); mMsgAdapter = new MsgPostAdapter(getActivity(), null, this); mMsgAdapter.notifyDataSetChanged(); getLatestPostsFromInternet(); mMsgListView.setOnItemClickListener(this); mMsgListView.setOnScrollListener( new OnScrollListener() { int preLastPos = -1; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_TOUCH_SCROLL) return; // 上拉判断一定要在下拉之前,否则数据量不足一屏幕的时候会有问题 int curLastPos = view.getLastVisiblePosition(); if (curLastPos >= view.getCount() - 1 && curLastPos != preLastPos) { Log.d("FirstPageFragment", "上拉加载更多----"); preLastPos = curLastPos; // 避免多次触发 showMoreCachedResultsFromAdapter(); } Log.d("FirstPageFragment", "lastVisiblePosition: " + view.getLastVisiblePosition()); Log.d("FirstPageFragment", "view.getCount - 1: " + (view.getCount() - 1)); } @Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {} }); footerView = View.inflate(getActivity(), R.layout.listview_footer, null); TextView textView = (TextView) footerView.findViewById(R.id.listview_footer_text); textView.setText("没有更多消息了..."); footerView.setClickable(false); mMsgListView.addHeaderView(View.inflate(getActivity(), R.layout.listview_header, null)); mMsgListView.setAdapter(mMsgAdapter); return view; }