9.indicate、xutils、json

json数据

 页签详情页数据  
  1. public class TabData {
  2. public int retcode;
  3. public TabDetail data;//不是数组的话类型就是这个
  4. public class TabDetail {
  5. public String title;
  6. public String more;
  7. public ArrayList<TabNewsData> news;
  8. public ArrayList<TopNewsData> topnews;
  9. @Override
  10. public String toString() {
  11. return "TabDetail [title=" + title + ", news=" + news
  12. + ", topnews=" + topnews + "]";
  13. }
  14. }
  15. /**
  16. * 新闻列表对象
  17. */
  18. public class TabNewsData {
  19. public String id;
  20. public String listimage;
  21. public String pubdate;
  22. public String title;
  23. public String type;
  24. public String url;
  25. @Override
  26. public String toString() {
  27. return "TabNewsData [title=" + title + "]";
  28. }
  29. }
  30. /**
  31. * 头条新闻
  32. */
  33. public class TopNewsData {
  34. public String id;
  35. public String topimage;
  36. public String pubdate;
  37. public String title;
  38. public String type;
  39. public String url;
  40. @Override
  41. public String toString() {
  42. return "TopNewsData [title=" + title + "]";
  43. }
  44. }
  45. @Override
  46. public String toString() {
  47. return "TabData [data=" + data + "]";
  48. }
  49. }
页签详情页
知识点:
  • 使用xutils加载网络图片,xutils的configDefaultLoadingImage()方法,设置默认图片 ,还有 BitmapFun 、ImageDownloader也能加载网络图片
  • xutils的注解,不需要fandviewbyid了,但是前提是需要注入事件ViewUtils.inject(this, view);
  • 可以给listview加个头,那样头也跟着滑动了,头上还能加头,lvList.addHeaderView(headerView);
  • indicate的使用


    1.list_header_topnews.xml:头条新闻

由于indicate自带的下面的几个点很难看,可以根据自己的需求修改
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"//注意命名空间也需要拷过来
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" >
  5. <com.itheima.zhbj52.view.TopNewsViewPager
  6. android:id="@+id/vp_news"
  7. android:layout_width="match_parent"
  8. android:layout_height="200dp" />
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentBottom="true"
  13. android:background="#a000"
  14. android:padding="3dp" >
  15. <TextView
  16. android:id="@+id/tv_title"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:textColor="#fff"
  20. android:textSize="16sp" />
  21. <com.viewpagerindicator.CirclePageIndicator//下面的几个点
  22. android:id="@+id/indicator"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignParentRight="true"
  26. android:padding="10dip"
  27. app:fillColor="#f00"//点选中的颜色
  28. app:pageColor="@android:color/darker_gray"//点默认的颜色
  29. app:radius="3dp"//大小
  30. app:strokeWidth="0dp" />//点外面的黑圆圈
  31. </RelativeLayout>
  32. </RelativeLayout>

2.list_news_item.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:padding="10dp"
  5. >
  6. <ImageView
  7. android:id="@+id/iv_pic"
  8. android:layout_width="110dp"
  9. android:layout_height="70dp"
  10. android:scaleType="fitXY"
  11. android:padding="1dp"
  12. android:background="@android:color/darker_gray"//padding了一个dp,背景是灰色的,图片外就显示出了一个边框
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentTop="true"
  15. android:src="@drawable/image_demo" />
  16. <TextView
  17. android:id="@+id/tv_title"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentTop="true"
  21. android:layout_marginLeft="20dp"
  22. android:layout_toRightOf="@+id/iv_pic"
  23. android:text="新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题"
  24. android:textColor="#000"
  25. android:maxLines="2"
  26. android:ellipsize="end"
  27. android:textSize="20sp" />
  28. <TextView
  29. android:id="@+id/tv_date"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_alignLeft="@id/tv_title"
  33. android:text="2015-03-16 16:20"
  34. android:layout_alignBottom="@id/iv_pic"
  35. android:textColor="@android:color/darker_gray"
  36. android:textSize="16sp" />
  37. </RelativeLayout>
3、tab_detail_pager.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <com.itheima.zhbj52.view.RefreshListView
  6. android:id="@+id/lv_list"
  7. android:layout_width="match_parent"
  8. android:layout_height="0dp"
  9. android:cacheColorHint="#fff"
  10. android:layout_weight="1" >
  11. </com.itheima.zhbj52.view.RefreshListView>
  12. </LinearLayout>
4.页签详情页

  1. public class TabDetailPager extends BaseMenuDetailPager implements
  2. OnPageChangeListener {
    1. NewsTabData mTabData;// 新闻页签数据
    2. // private TextView tvText;
    3. private String mUrl;// 访问页签的网络地址
    4. private TabData mTabDetailData;// 页签详情页数据
    5. @ViewInject(R.id.vp_news)
    6. private ViewPager mViewPager;// 头条新闻的pager
    7. @ViewInject(R.id.tv_title)
    8. private TextView tvTitle;// 头条新闻的标题
    9. private ArrayList<TopNewsData> mTopNewsList;// 头条新闻数据集合
    10. @ViewInject(R.id.indicator)
    11. private CirclePageIndicator mIndicator;// 头条新闻位置指示器
    12. @ViewInject(R.id.lv_list)
    13. private RefreshListView lvList;// 新闻列表
    14. private ArrayList<TabNewsData> mNewsList; // 新闻数据集合
    15. private NewsAdapter mNewsAdapter;// 给list设置的adapter

  3. public TabDetailPager(Activity activity, NewsTabData newsTabData) {
  4. super(activity);
  5. mTabData = newsTabData;//new的时候把数据和连接地址也创建了
  6. mUrl = GlobalContants.SERVER_URL + mTabData.url;
  7. }
  8. @Override
  9. public View initViews() {
  10. View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);//下拉刷新
  11. // 加载头布局
  12. View headerView = View.inflate(mActivity, R.layout.list_header_topnews,
  13. null);
  14. ViewUtils.inject(this, view);
  15. ViewUtils.inject(this, headerView);
  16. // 将头条新闻以头布局的形式加给listview
  17. lvList.addHeaderView(headerView);
  18. return view;
  19. }
  20. @Override
  21. public void initData() {
  22. getDataFromServer();
  23. }
  24. private void getDataFromServer() {
  25. HttpUtils utils = new HttpUtils();
  26. utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>() {
  27. @Override
  28. public void onSuccess(ResponseInfo<String> responseInfo) {
  29. String result = (String) responseInfo.result;
  30. System.out.println("页签详情页返回结果:" + result);
  31. parseData(result);
  32. }
  33. @Override
  34. public void onFailure(HttpException error, String msg) {
  35. Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
  36. error.printStackTrace();
  37. }
  38. });
  39. }
  40. protected void parseData(String result) {
  41. Gson gson = new Gson();
  42. mTabDetailData = gson.fromJson(result, TabData.class);
  43. System.out.println("页签详情解析:" + mTabDetailData);
  44. mTopNewsList = mTabDetailData.data.topnews;
  45. mNewsList = mTabDetailData.data.news;
  46. if (mTopNewsList != null) {
  47. mViewPager.setAdapter(new TopNewsAdapter());
  48. mIndicator.setViewPager(mViewPager);
  49. mIndicator.setSnap(true);// 支持快照显示,即不是慢慢的滑过去,而是跳过去
  50. mIndicator.setOnPageChangeListener(this);
  51. mIndicator.onPageSelected(0);// 让指示器重新定位到第一个点
  52. tvTitle.setText(mTopNewsList.get(0).title);//首先让他显示个字
  53. }
  54. if (mNewsList != null) {
  55. mNewsAdapter = new NewsAdapter();
  56. lvList.setAdapter(mNewsAdapter);
  57. }
  58. }
  59. /**
  60. * 头条新闻适配器
  61. *
  62. * @author Kevin
  63. *
  64. */
  65. class TopNewsAdapter extends PagerAdapter {
  66. private BitmapUtils utils;
  67. public TopNewsAdapter() {
  68. utils = new BitmapUtils(mActivity);
  69. utils.configDefaultLoadingImage(R.drawable.topnews_item_default);// 设置默认图片
  70. }
  71. @Override
  72. public int getCount() {
  73. return mTabDetailData.data.topnews.size();
  74. }
  75. @Override
  76. public boolean isViewFromObject(View arg0, Object arg1) {
  77. return arg0 == arg1;
  78. }
  79. @Override
  80. public Object instantiateItem(ViewGroup container, int position) {
  81. ImageView image = new ImageView(mActivity);
  82. image.setScaleType(ScaleType.FIT_XY);// 基于控件大小填充图片
  83. TopNewsData topNewsData = mTopNewsList.get(position);
  84. utils.display(image, topNewsData.topimage);// 传递imagView对象和图片地址
  85. container.addView(image);
  86. System.out.println("instantiateItem....." + position);
  87. return image;
  88. }
  89. @Override
  90. public void destroyItem(ViewGroup container, int position, Object object) {
  91. container.removeView((View) object);
  92. }
  93. }
  94. /**
  95. * 新闻列表的适配器
  96. *
  97. * @author Kevin
  98. *
  99. */
  100. class NewsAdapter extends BaseAdapter {
  101. private BitmapUtils utils;
  102. public NewsAdapter() {
  103. utils = new BitmapUtils(mActivity);
  104. utils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
  105. }
  106. @Override
  107. public int getCount() {
  108. return mNewsList.size();
  109. }
  110. @Override
  111. public TabNewsData getItem(int position) {
  112. return mNewsList.get(position);
  113. }
  114. @Override
  115. public long getItemId(int position) {
  116. return position;
  117. }
  118. @Override
  119. public View getView(int position, View convertView, ViewGroup parent) {
  120. ViewHolder holder;
  121. if (convertView == null) {
  122. convertView = View.inflate(mActivity, R.layout.list_news_item,
  123. null);
  124. holder = new ViewHolder();
  125. holder.ivPic = (ImageView) convertView
  126. .findViewById(R.id.iv_pic);
  127. holder.tvTitle = (TextView) convertView
  128. .findViewById(R.id.tv_title);
  129. holder.tvDate = (TextView) convertView
  130. .findViewById(R.id.tv_date);
  131. convertView.setTag(holder);
  132. } else {
  133. holder = (ViewHolder) convertView.getTag();
  134. }
  135. TabNewsData item = getItem(position);
  136. holder.tvTitle.setText(item.title);
  137. holder.tvDate.setText(item.pubdate);
  138. utils.display(holder.ivPic, item.listimage);
  139. return convertView;
  140. }
  141. }
  142. static class ViewHolder {
  143. public TextView tvTitle;
  144. public TextView tvDate;
  145. public ImageView ivPic;
  146. }
  147. @Override
  148. public void onPageScrollStateChanged(int arg0) {
  149. }
  150. @Override
  151. public void onPageScrolled(int arg0, float arg1, int arg2) {
  152. }
  153. @Override
  154. public void onPageSelected(int arg0) {
  155. TopNewsData topNewsData = mTopNewsList.get(arg0);
  156. tvTitle.setText(topNewsData.title);
  157. }
  158. }








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