// 存储TimeLine到SharedPreferences中 private void saveTimeline() { S.put(getActivity(), "timeline_list", ""); int count = listPostAdapter.getCount() > 10 ? 10 : listPostAdapter.getCount(); // 只保存前10条 String targetString = ""; String seperator = S.regularEx; for (int i = 0; i < count; i++) { PostBean bean = (PostBean) listPostAdapter.getItem(i); // 1id 2链接 3标题 4作者 5时间 6图片地址 7头图URL 8注释 9类型 targetString = targetString + seperator + String.valueOf(bean.getId()) + seperator + bean.getItemURL() + seperator + bean.getTitle() + seperator + bean.getAuthor() + seperator + bean.getCreatedAt() + seperator + bean.getImageUrl() + seperator + bean.getHeaderImageUrl() + seperator + bean.getDescription() + seperator + bean.getContentType(); } S.put(getActivity(), "timeline_list", targetString); }
// 解析Timeline接口返回的数据 private void handlePostExecute(String result, int type) { if (!isNetworkConnected(getActivity())) { Util.showToast(getActivity(), "没有可用网络,请检查网络设置~"); listView.onRefreshComplete(); } else if (result == "") { Util.showToast(getActivity(), "网络开小差了,不如再试试吧~"); listView.onRefreshComplete(); } else { LinkedList<PostBean> postBeans = PostBean.parseBabietaTimeline(result, getActivity()); if (type == 0) { listPostAdapter.clearPost(); // 上拉刷新,清空原来的数据 listPostAdapter.appendPost(postBeans); listPostAdapter.sortPost(); saveTimeline(); } else { // 下拉刷新,直接添加数据 listPostAdapter.appendPost(postBeans); listPostAdapter.sortPost(); } listPostAdapter.notifyDataSetInvalidated(); TextView textView = (TextView) pageView.findViewById(R.id.vp_main_text); if (listPostAdapter.getCount() == 0) { textView.setText("还没有内容,下拉刷新看看~"); } else { textView.setText("新闻列表"); textView.setVisibility(View.GONE); } listView.onRefreshComplete(); } }
// Fragment在不可见的时候会回收所有View,所以在出栈的时候上一个Fragment需要重新初始化View @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.view = inflater.inflate(R.layout.index_timeline, container, false); this.initPostListView(); // Timeline部分 this.initPageView(); // Focus部分 if (focusLoadedFlag == false) requestForFocus(); listPostAdapter.notifyDataSetChanged(); TextView textView = (TextView) pageView.findViewById(R.id.vp_main_text); if (listPostAdapter.getCount() == 0) { textView.setText("下拉刷新看看~"); } else { textView.setText("新闻列表"); textView.setVisibility(View.GONE); } return view; }