// 存储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; }
// 从本地缓存读取TimeLine private boolean loadTimeline() { LinkedList<PostBean> postBeans = new LinkedList<PostBean>(); String[] collectlist = S.getStringSet(getActivity(), "timeline_list"); for (int i = 1; i < (collectlist.length); i++) { // System.out.println(collectlist[i]); } for (int i = 1; i < (collectlist.length); ) { PostBean postBean = new PostBean(); try { Integer.valueOf(collectlist[i]); } catch (Exception e) { e.printStackTrace(); S.put(getActivity(), "timeline_list", ""); return false; } postBean.setId(Integer.valueOf(collectlist[i])); postBean.setItemURL(collectlist[i + 1]); postBean.setTitle(collectlist[i + 2]); postBean.setAuthor(collectlist[i + 3]); postBean.setUpdatedAt(collectlist[i + 4]); postBean.setCreatedAt(collectlist[i + 4]); // (注意这个时间同updated_at) postBean.setImageUrl(collectlist[i + 5]); postBean.setHeaderImageUrl(collectlist[i + 6]); postBean.setDescription(collectlist[i + 7]); postBean.setContentType(collectlist[i + 8]); postBeans.add(postBean); // 添加到链表 i = i + 9; } listPostAdapter.appendPost(postBeans); listPostAdapter.sortPost(); return true; }