10.下拉刷新、加载更多、标记已读、轮播条、缓存

下拉刷新、加载更多、标记已读、轮播条、缓存
下拉刷新-------
1.addHeaderView必须在setAdapter之前调用
2.将paddingTop设置一个headerView高度的负值去隐藏它
getHeight()和getMeasuredHeight()的区别:
getMeasuredHeight():获取测量完的高度,只要在onMeasure方法执行完,就可以用
                   它获取到宽高,在自定义控件内部多使用这个
使用view.measure(0,0)方法可以主动通知系统去测量,然后就
可以直接使用它获取宽高
getHeight():必须在onLayout方法执行完后,才能获得宽高
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int headerViewHeight = headerView.getHeight();
//直接可以获取宽高
}
});
3.setSelection(position);将对应位置的item放置到屏幕顶端

下拉刷新的原理:

1.自定义带下拉刷新,加载更多的listview
  1. public class RefreshListView extends ListView implements OnScrollListener,
  2. android.widget.AdapterView.OnItemClickListener {
  3. private static final int STATE_PULL_REFRESH = 0;// 下拉刷新
  4. private static final int STATE_RELEASE_REFRESH = 1;// 松开刷新
  5. private static final int STATE_REFRESHING = 2;// 正在刷新
  6. private View mHeaderView;
  7. private int startY = -1;// 滑动起点的y坐标
  8. private int mHeaderViewHeight;
  9. private int mCurrrentState = STATE_PULL_REFRESH;// 当前状态
  10. private TextView tvTitle;
  11. private TextView tvTime;
  12. private ImageView ivArrow;
  13. private ProgressBar pbProgress;
  14. private RotateAnimation animUp;
  15. private RotateAnimation animDown;
  16. public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
  17. super(context, attrs, defStyle);
  18. initHeaderView();
  19. initFooterView();
  20. }
  21. public RefreshListView(Context context, AttributeSet attrs) {
  22. super(context, attrs);
  23. initHeaderView();
  24. initFooterView();
  25. }
  26. public RefreshListView(Context context) {
  27. super(context);
  28. initHeaderView();
  29. initFooterView();
  30. }
  31. /**
  32. * 初始化头布局
  33. */
  34. private void initHeaderView() {
  35. mHeaderView = View.inflate(getContext(), R.layout.refresh_header, null);
  36. this.addHeaderView(mHeaderView);
  37. tvTitle = (TextView) mHeaderView.findViewById(R.id.tv_title);
  38. tvTime = (TextView) mHeaderView.findViewById(R.id.tv_time);
  39. ivArrow = (ImageView) mHeaderView.findViewById(R.id.iv_arr);
  40. pbProgress = (ProgressBar) mHeaderView.findViewById(R.id.pb_progress);
  41. mHeaderView.measure(0, 0);
  42. //测量之后该view的getMeasuredHeight()就会返回刚才测量所得的高,getMeasuredWidth返回测量所得宽
  43. mHeaderViewHeight = mHeaderView.getMeasuredHeight();
  44. mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);// 隐藏头布局
  45. initArrowAnim();
  46. tvTime.setText("最后刷新时间:" + getCurrentTime());
  47. }
  48. /*
  49. * 初始化脚布局
  50. */
  51. private void initFooterView() {
  52. mFooterView = View.inflate(getContext(),
  53. R.layout.refresh_listview_footer, null);
  54. this.addFooterView(mFooterView);
  55. mFooterView.measure(0, 0);
  56. mFooterViewHeight = mFooterView.getMeasuredHeight();
  57. mFooterView.setPadding(0, -mFooterViewHeight, 0, 0);// 隐藏
  58. this.setOnScrollListener(this);//加载更多需要实现OnScrollListener接口
  59. }
  60. @Override
  61. public boolean onTouchEvent(MotionEvent ev) {//下拉刷新需要重写这个方法:触摸事件
  62. switch (ev.getAction()) {
  63. case MotionEvent.ACTION_DOWN:
  64. startY = (int) ev.getRawY();
  65. break;
  66. case MotionEvent.ACTION_MOVE:
  67. if (startY == -1) {// 确保startY有效,有时候ACTION_DOWN事件不会调用,需要重新掉一次
  68. startY = (int) ev.getRawY();
  69. }
  70. if (mCurrrentState == STATE_REFRESHING) {// 正在刷新时不做处理
  71. break;
  72. }
  73. int endY = (int) ev.getRawY();
  74. int dy = endY - startY;// 移动便宜量
  75. if (dy > 0 && getFirstVisiblePosition() == 0) {// 只有下拉并且当前是第一个item,才允许下拉
  76. int padding = dy - mHeaderViewHeight;// 计算padding
  77. mHeaderView.setPadding(0, padding, 0, 0);// 设置当前padding
  78. if (padding > 0 && mCurrrentState != STATE_RELEASE_REFRESH) {// 状态改为松开刷新,头布局全部出来了
  79. mCurrrentState = STATE_RELEASE_REFRESH;
  80. refreshState();
  81. } else if (padding < 0 && mCurrrentState != STATE_PULL_REFRESH) {// 改为下拉刷新状态
  82. mCurrrentState = STATE_PULL_REFRESH;
  83. refreshState();
  84. }
  85. return true;
  86. }
  87. break;
  88. case MotionEvent.ACTION_UP:
  89. startY = -1;// 重置
  90. if (mCurrrentState == STATE_RELEASE_REFRESH) {
  91. mCurrrentState = STATE_REFRESHING;// 正在刷新
  92. mHeaderView.setPadding(0, 0, 0, 0);// 显示
  93. refreshState();
  94. } else if (mCurrrentState == STATE_PULL_REFRESH) {
  95. mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);// 隐藏
  96. }
  97. break;
  98. default:
  99. break;
  100. }
  101. return super.onTouchEvent(ev);
  102. }
  103. /**
  104. * 刷新下拉控件的布局
  105. */
  106. private void refreshState() {
  107. switch (mCurrrentState) {
  108. case STATE_PULL_REFRESH:
  109. tvTitle.setText("下拉刷新");
  110. ivArrow.setVisibility(View.VISIBLE);
  111. pbProgress.setVisibility(View.INVISIBLE);
  112. ivArrow.startAnimation(animDown);//箭头向下
  113. break;
  114. case STATE_RELEASE_REFRESH:
  115. tvTitle.setText("松开刷新");
  116. ivArrow.setVisibility(View.VISIBLE);
  117. pbProgress.setVisibility(View.INVISIBLE);
  118. ivArrow.startAnimation(animUp);
  119. break;
  120. case STATE_REFRESHING:
  121. tvTitle.setText("正在刷新...");
  122. ivArrow.clearAnimation();// 必须先清除动画,才能隐藏
  123. ivArrow.setVisibility(View.INVISIBLE);
  124. pbProgress.setVisibility(View.VISIBLE);
  125. if (mListener != null) {
  126. mListener.onRefresh();//不等于空时才刷新数据
  127. }
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. /**
  134. * 初始化箭头动画
  135. */
  136. private void initArrowAnim() {
  137. // 箭头向上动画
  138. animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
  139. Animation.RELATIVE_TO_SELF, 0.5f);
  140. animUp.setDuration(200);
  141. animUp.setFillAfter(true);
  142. // 箭头向下动画
  143. animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
  144. 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  145. animDown.setDuration(200);
  146. animDown.setFillAfter(true);
  147. }
  148. OnRefreshListener mListener;
  149. private View mFooterView;
  150. private int mFooterViewHeight;
  151. public void setOnRefreshListener(OnRefreshListener listener) {
  152. mListener = listener;
  153. }
  154. public interface OnRefreshListener {//定义个接口
  155. public void onRefresh();
  156. public void onLoadMore();// 加载下一页数据
  157. }
  158. /*
  159. * 收起下拉刷新的控件
  160. */
  161. public void onRefreshComplete(boolean success) {//加了个参数,成功才更新时间
  162. if (isLoadingMore) {// 正在加载更多...如果是加载下一页
  163. mFooterView.setPadding(0, -mFooterViewHeight, 0, 0);// 隐藏脚布局,
  164. isLoadingMore = false;
  165. } else {
  166. mCurrrentState = STATE_PULL_REFRESH;
  167. tvTitle.setText("下拉刷新");
  168. ivArrow.setVisibility(View.VISIBLE);
  169. pbProgress.setVisibility(View.INVISIBLE);
  170. mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);// 隐藏
  171. if (success) {
  172. tvTime.setText("最后刷新时间:" + getCurrentTime());
  173. }
  174. }
  175. }
  176. /**
  177. * 获取当前时间
  178. */
  179. public String getCurrentTime() {
  180. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  181. return format.format(new Date());
  182. }
  183. //定义个标记,要不然一直掉这个到底了,判断现在是否加载了
  184. private boolean isLoadingMore;
  185. @Override
  186. public void onScrollStateChanged(AbsListView view, int scrollState) {
  187. if (scrollState == SCROLL_STATE_IDLE
  188. || scrollState == SCROLL_STATE_FLING) {//空闲和快速滑状态
  189. if (getLastVisiblePosition() == getCount() - 1 && !isLoadingMore) {// 滑动到最后,默认是FALSE,然后置为TRUE
  190. System.out.println("到底了.....");
  191. mFooterView.setPadding(0, 0, 0, 0);// 显示
  192. setSelection(getCount() - 1);// 改变listview显示位置
  193. isLoadingMore = true;//致为TRUE,下次进来不调用了,可是如果还有数据,什么时候致为FALSE了?在onRefreshComplete方法里,加载结束后
  194. if (mListener != null) {
  195. mListener.onLoadMore();// 在这里调用这个方法:加载下一页数据
  196. }
  197. }
  198. }
  199. }
  200. @Override
  201. public void onScroll(AbsListView view, int firstVisibleItem,
  202. int visibleItemCount, int totalItemCount) {
  203. }
  204. OnItemClickListener mItemClickListener;
  205. @Override
  206. public void setOnItemClickListener(//重写listview的setOnItemClickListener方法
  207. android.widget.AdapterView.OnItemClickListener listener) {
  208. super.setOnItemClickListener(this);//改成this,并且实现OnItemClickListener 接口
  209. mItemClickListener = listener;
  210. }
  211. @Override
  212. public void onItemClick(AdapterView<?> parent, View view, int position,
  213. long id) {
  214. if (mItemClickListener != null) {//这样就调用地方的点击条目就变成了减去头view后重新开始的position了
  215. mItemClickListener.onItemClick(parent, view, position
  216. - getHeaderViewsCount(), id);
  217. }
  218. }
  219. }
2.头布局
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal" >
  5. <FrameLayout
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:padding="10dp" >
  9. <ImageView
  10. android:id="@+id/iv_arr"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="center"
  14. android:src="@drawable/common_listview_headview_red_arrow" />
  15. <ProgressBar
  16. android:id="@+id/pb_progress"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center"
  20. android:indeterminateDrawable="@drawable/custom_progress"
  21. android:visibility="invisible" />
  22. </FrameLayout>
  23. <LinearLayout
  24. android:layout_width="wrap_content"
  25. android:layout_height="match_parent"
  26. android:layout_gravity="center"
  27. android:gravity="center"
  28. android:orientation="vertical" >
  29. <TextView
  30. android:id="@+id/tv_title"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="下拉刷新"
  34. android:textColor="#f00"
  35. android:textSize="16sp" />
  36. <TextView
  37. android:id="@+id/tv_time"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_marginTop="5dp"
  41. android:text="最后刷新时间:2015-03-10 17:07:07"
  42. android:textColor="@android:color/darker_gray"
  43. android:textSize="14sp" />
  44. </LinearLayout>
  45. </LinearLayout>
custom_progress:改变ProgressBar的样子,这里在drawable里自定义了一个圆圈
  1. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:fromDegrees="0"
  3. android:pivotX="50%"
  4. android:pivotY="50%"
  5. android:toDegrees="360" >
  6. <shape
  7. android:innerRadius="12dp"//圈内半径
  8. android:shape="ring"//圆环
  9. android:thickness="3dp"//圆圈厚度
  10. android:useLevel="false" >
  11. <gradient
  12. android:centerColor="#3f00"//开始,粉色,3是透明度
  13. android:endColor="#f00"//中间
  14. android:startColor="#fff" />//结束,循环转圈
  15. </shape>
  16. </rotate>

3.脚布局
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:gravity="center"
  5. android:orientation="horizontal" >
  6. <ProgressBar
  7. android:id="@+id/pb_pull_list_header"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center"
  11. android:layout_margin="5dp"
  12. android:indeterminateDrawable="@drawable/custom_progress" />
  13. <TextView
  14. android:id="@+id/tv_pull_list_header_title"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="加载中..."
  18. android:textColor="#ff0000"
  19. android:textSize="18sp" />
  20. </LinearLayout>
4.在页签详情页中调用自定义listview的方法
  1. public class TabDetailPager extends BaseMenuDetailPager implements
  2. OnPageChangeListener {
  3. 。。。
  4. private String mMoreUrl;// 更多页面的地址,数据里有的话才加载下一页

  5. @Override
  6. public View initViews() {
  7. View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
  8. // 加载头布局
  9. View headerView = View.inflate(mActivity, R.layout.list_header_topnews,
  10. null);
  11. ViewUtils.inject(this, view);
  12. ViewUtils.inject(this, headerView);
  13. // 将头条新闻以头布局的形式加给listview
  14. lvList.addHeaderView(headerView);
  15. // 设置下拉刷新监听
  16. lvList.setOnRefreshListener(new OnRefreshListener() {
  17. @Override
  18. public void onRefresh() {
  19. getDataFromServer();
  20. }
  21. @Override
  22. public void onLoadMore() {
  23. if (mMoreUrl != null) {
  24. getMoreDataFromServer();//调用more接口
  25. } else {
  26. Toast.makeText(mActivity, "最后一页了", Toast.LENGTH_SHORT)
  27. .show();
  28. // 收起加载更多的布局,传false和TRUE一样,只不过是是否更新更新时间的textview
  29. lvList.onRefreshComplete(false);
  30. }
  31. }
  32. });
  33. lvList.setOnItemClickListener(new OnItemClickListener() {
  34. @Override
  35. public void onItemClick(AdapterView<?> parent, View view,
  36. int position, long id) {
  37. System.out.println("被点击:" + position);
  38. // 35311,34221,34234,34342
  39. // 在本地记录已读状态
  40. String ids = PrefUtils.getString(mActivity, "read_ids", "");
  41. String readId = mNewsList.get(position).id;
  42. if (!ids.contains(readId)) {
  43. ids = ids + readId + ",";
  44. PrefUtils.setString(mActivity, "read_ids", ids);
  45. }
  46. // mNewsAdapter.notifyDataSetChanged();//这样写也可以,不过重新调用getview方法
  47. // 实现局部界面刷新, 这个view就是被点击的item布局对象,注意这是个好方法
  48. changeReadState(view);
  49. // 跳转新闻详情页
  50. Intent intent = new Intent();
  51. intent.setClass(mActivity, NewsDetailActivity.class);
  52. intent.putExtra("url", mNewsList.get(position).url);
  53. mActivity.startActivity(intent);
  54. }
  55. });
  56. return view;
  57. }
  58. /**
  59. * 改变已读新闻的颜色
  60. */
  61. private void changeReadState(View view) {
  62. TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
  63. tvTitle.setTextColor(Color.GRAY);
  64. }
  65. @Override
  66. public void initData() {
  67. String cache = CacheUtils.getCache(mUrl, mActivity);//读取缓存
  68. if (!TextUtils.isEmpty(cache)) {
  69. parseData(cache, false);
  70. }
  71. getDataFromServer();//不管用没有缓存都请求下服务器,看有没有最新的
  72. }
  73. private void getDataFromServer() {
  74. HttpUtils utils = new HttpUtils();
  75. utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>() {
  76. @Override
  77. public void onSuccess(ResponseInfo<String> responseInfo) {
  78. String result = (String) responseInfo.result;
  79. System.out.println("页签详情页返回结果:" + result);
  80. parseData(result, false);
  81. lvList.onRefreshComplete(true);
  82. // 设置缓存
  83. CacheUtils.setCache(mUrl, result, mActivity);
  84. }
  85. @Override
  86. public void onFailure(HttpException error, String msg) {
  87. Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
  88. error.printStackTrace();
  89. lvList.onRefreshComplete(false)
  90. }
  91. });
  92. }
  93. /**
  94. * 加载下一页数据
  95. */
  96. private void getMoreDataFromServer() {
  97. HttpUtils utils = new HttpUtils();
  98. utils.send(HttpMethod.GET, mMoreUrl, new RequestCallBack<String>() {
  99. @Override
  100. public void onSuccess(ResponseInfo<String> responseInfo) {
  101. String result = (String) responseInfo.result;
  102. parseData(result, true);
  103. lvList.onRefreshComplete(true);
  104. }
  105. @Override
  106. public void onFailure(HttpException error, String msg) {
  107. Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
  108. error.printStackTrace();
  109. lvList.onRefreshComplete(false);
  110. }
  111. });
  112. }
  113. protected void parseData(String result, boolean isMore) {
  114. Gson gson = new Gson();
  115. mTabDetailData = gson.fromJson(result, TabData.class);
  116. System.out.println("页签详情解析:" + mTabDetailData);
  117. // 处理下一页链接
  118. String more = mTabDetailData.data.more;
  119. if (!TextUtils.isEmpty(more)) {
  120. mMoreUrl = GlobalContants.SERVER_URL + more;
  121. } else {
  122. mMoreUrl = null;
  123. }
  124. if (!isMore) {
  125. mTopNewsList = mTabDetailData.data.topnews;
  126. mNewsList = mTabDetailData.data.news;
  127. if (mTopNewsList != null) {
  128. mViewPager.setAdapter(new TopNewsAdapter());
  129. mIndicator.setViewPager(mViewPager);
  130. mIndicator.setSnap(true);// 支持快照显示
  131. mIndicator.setOnPageChangeListener(this);
  132. mIndicator.onPageSelected(0);// 让指示器重新定位到第一个点
  133. tvTitle.setText(mTopNewsList.get(0).title);
  134. }
  135. if (mNewsList != null) {
  136. mNewsAdapter = new NewsAdapter();
  137. lvList.setAdapter(mNewsAdapter);
  138. }
  139. // 自动轮播条显示
  140. if (mHandler == null) {
  141. mHandler = new Handler() {
  142. public void handleMessage(android.os.Message msg) {
  143. int currentItem = mViewPager.getCurrentItem();
  144. if (currentItem < mTopNewsList.size() - 1) {
  145. currentItem++;
  146. } else {
  147. currentItem = 0;
  148. }
  149. mViewPager.setCurrentItem(currentItem);// 切换到下一个页面
  150. // 继续延时3秒发消息,形成循环,可以handleMessage方法里发送消息的
  151. mHandler.sendEmptyMessageDelayed(0, 3000);
  152. };
  153. };
  154. mHandler.sendEmptyMessageDelayed(0, 3000);// 延时3秒后发消息
  155. }
  156. } else {// 如果是加载下一页,需要将数据追加给原来的集合
  157. ArrayList<TabNewsData> news = mTabDetailData.data.news;
  158. mNewsList.addAll(news);//这个方法将数据追加给原来的list
  159. mNewsAdapter.notifyDataSetChanged();
  160. }
  161. }
  162. /**
  163. * 头条新闻适配器
  164. *
  165. * @author Kevin
  166. *
  167. */
  168. class TopNewsAdapter extends PagerAdapter {
  169. private BitmapUtils utils;
  170. public TopNewsAdapter() {
  171. utils = new BitmapUtils(mActivity);
  172. utils.configDefaultLoadingImage(R.drawable.topnews_item_default);// 设置默认图片
  173. }
  174. @Override
  175. public int getCount() {
  176. return mTabDetailData.data.topnews.size();
  177. }
  178. @Override
  179. public boolean isViewFromObject(View arg0, Object arg1) {
  180. return arg0 == arg1;
  181. }
  182. @Override
  183. public Object instantiateItem(ViewGroup container, int position) {
  184. ImageView image = new ImageView(mActivity);
  185. image.setScaleType(ScaleType.FIT_XY);// 基于控件大小填充图片
  186. TopNewsData topNewsData = mTopNewsList.get(position);
  187. utils.display(image, topNewsData.topimage);// 传递imagView对象和图片地址
  188. container.addView(image);
  189. image.setOnTouchListener(new TopNewsTouchListener());//设置触摸监听
  190. return image;
  191. }
  192. @Override
  193. public void destroyItem(ViewGroup container, int position, Object object) {
  194. container.removeView((View) object);
  195. }
  196. }
  197. /**
  198. * 头条新闻的触摸监听
  199. *
  200. * @author Kevin
  201. *
  202. */
  203. class TopNewsTouchListener implements OnTouchListener {
  204. @Override
  205. public boolean onTouch(View v, MotionEvent event) {
  206. switch (event.getAction()) {
  207. case MotionEvent.ACTION_DOWN:
  208. System.out.println("按下");
  209. // 删除Handler中的所有消息,Callbacks是说有个postDelayed,传的是Runnable,这么这个方法每几秒执行一次
  210. mHandler.removeCallbacksAndMessages(null);
  211. // mHandler.postDelayed(new Runnable() {
  212. //
  213. // @Override
  214. // public void run() {
  215. //
  216. // }
  217. // }, 3000);
  218. break;
  219. case MotionEvent.ACTION_CANCEL:
  220. //因为当按下没抬起,而是滑了一下,那么事件就取消了,需要重新发送一下
  221. System.out.println("事件取消");
  222. mHandler.sendEmptyMessageDelayed(0, 3000);
  223. break;
  224. case MotionEvent.ACTION_UP:
  225. System.out.println("抬起");
  226. mHandler.sendEmptyMessageDelayed(0, 3000);
  227. break;
  228. default:
  229. break;
  230. }
  231. return true;
  232. }
  233. }
  234. /**
  235. * 新闻列表的适配器
  236. *
  237. * @author Kevin
  238. *
  239. */
  240. class NewsAdapter extends BaseAdapter {
  241. private BitmapUtils utils;
  242. public NewsAdapter() {
  243. utils = new BitmapUtils(mActivity);
  244. utils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
  245. }
  246. @Override
  247. public int getCount() {
  248. return mNewsList.size();
  249. }
  250. @Override
  251. public TabNewsData getItem(int position) {
  252. return mNewsList.get(position);
  253. }
  254. @Override
  255. public long getItemId(int position) {
  256. return position;
  257. }
  258. @Override
  259. public View getView(int position, View convertView, ViewGroup parent) {
  260. ViewHolder holder;
  261. if (convertView == null) {
  262. convertView = View.inflate(mActivity, R.layout.list_news_item,
  263. null);
  264. holder = new ViewHolder();
  265. holder.ivPic = (ImageView) convertView
  266. .findViewById(R.id.iv_pic);
  267. holder.tvTitle = (TextView) convertView
  268. .findViewById(R.id.tv_title);
  269. holder.tvDate = (TextView) convertView
  270. .findViewById(R.id.tv_date);
  271. convertView.setTag(holder);
  272. } else {
  273. holder = (ViewHolder) convertView.getTag();
  274. }
  275. TabNewsData item = getItem(position);
  276. holder.tvTitle.setText(item.title);
  277. holder.tvDate.setText(item.pubdate);
  278. utils.display(holder.ivPic, item.listimage);
  279. String ids = PrefUtils.getString(mActivity, "read_ids", "");//保存缓存
  280. if (ids.contains(getItem(position).id)) {
  281. holder.tvTitle.setTextColor(Color.GRAY);
  282. } else {
  283. holder.tvTitle.setTextColor(Color.BLACK);
  284. }
  285. return convertView;
  286. }
  287. }
  288. static class ViewHolder {
  289. public TextView tvTitle;
  290. public TextView tvDate;
  291. public ImageView ivPic;
  292. }
  293. @Override
  294. public void onPageScrollStateChanged(int arg0) {
  295. }
  296. @Override
  297. public void onPageScrolled(int arg0, float arg1, int arg2) {
  298. }
  299. @Override
  300. public void onPageSelected(int arg0) {
  301. TopNewsData topNewsData = mTopNewsList.get(arg0);
  302. tvTitle.setText(topNewsData.title);
  303. }
  304. }









posted @ 2015-10-28 22:38  梦和远方  阅读(352)  评论(0编辑  收藏  举报