zoukankan      html  css  js  c++  java
  • 12、json、GridView、缓存


    1.解析json数据:
    1. public class PhotosData {
    2. public int retcode;
    3. public PhotosInfo data;
    4. public class PhotosInfo {
    5. public String title;
    6. public ArrayList<PhotoInfo> news;
    7. }
    8. public class PhotoInfo {
    9. public String id;
    10. public String listimage;
    11. public String pubdate;
    12. public String title;
    13. public String type;
    14. public String url;
    15. }
    16. }

    2.布局:
    1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent" >
    4. <ListView
    5. android:id="@+id/lv_photo"
    6. android:layout_width="match_parent"
    7. android:layout_height="match_parent"
    8. android:cacheColorHint="#fff"
    9. android:divider="@null" />
    10. <GridView
    11. android:id="@+id/gv_photo"
    12. android:layout_width="match_parent"
    13. android:layout_height="match_parent"
    14. android:numColumns="2"
    15. android:visibility="gone" />
    16. </FrameLayout>
    使用同一个子布局,同一个adapter
    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="vertical" >
    5. <LinearLayout
    6. android:layout_width="match_parent"
    7. android:layout_height="wrap_content"
    8. android:layout_margin="10dp"
    9. android:background="@drawable/pic_list_item_bg"
    10. android:gravity="center"
    11. android:orientation="vertical" >
    12. <ImageView
    13. android:id="@+id/iv_pic"
    14. android:layout_width="match_parent"
    15. android:layout_height="180dp"
    16. android:scaleType="centerCrop"
    17. android:src="@drawable/news_pic_default" />
    18. <TextView
    19. android:id="@+id/tv_title"
    20. android:layout_width="match_parent"
    21. android:layout_height="wrap_content"
    22. android:padding="10dp"
    23. android:text="标题"
    24. android:singleLine="true"
    25. android:textColor="#000"
    26. android:textSize="22sp" />
    27. </LinearLayout>
    28. </LinearLayout>
    3.菜单详情页—组图
    1. public static final String PHOTOS_URL = SERVER_URL
    2. + "/photos/photos_1.json";// 获取组图信息的接口
    1. public class PhotoMenuDetailPager extends BaseMenuDetailPager {
    2. private ListView lvPhoto;
    3. private GridView gvPhoto;
    4. private ArrayList<PhotoInfo> mPhotoList;
    5. private PhotoAdapter mAdapter;
    6. private ImageButton btnPhoto;
    7. public PhotoMenuDetailPager(Activity activity, ImageButton btnPhoto) { super(activity);
    8. this.btnPhoto = btnPhoto;
    9. //这样就把imagebutton传了过来,好方法。在构造函数里传
    10. btnPhoto.setOnClickListener(new OnClickListener() {
    11. @Override
    12. public void onClick(View v) {
    13. changeDisplay();
    14. }
    15. });
    16. }
    17. @Override
    18. public View initViews() {
    19. View view = View.inflate(mActivity, R.layout.menu_photo_pager, null);
    20. lvPhoto = (ListView) view.findViewById(R.id.lv_photo);
    21. gvPhoto = (GridView) view.findViewById(R.id.gv_photo);
    22. return view;
    23. }
    24. @Override
    25. public void initData() {
    26. String cache = CacheUtils
    27. .getCache(GlobalContants.PHOTOS_URL, mActivity);
    28. if (!TextUtils.isEmpty(cache)) {
    29. }
    30. getDataFromServer();
    31. }
    32. private void getDataFromServer() {
    33. HttpUtils utils = new HttpUtils();
    34. utils.send(HttpMethod.GET, GlobalContants.PHOTOS_URL,
    35. new RequestCallBack<String>() {
    36. @Override
    37. public void onSuccess(ResponseInfo<String> responseInfo) {
    38. String result = (String) responseInfo.result;
    39. parseData(result);
    40. // 设置缓存
    41. CacheUtils.setCache(GlobalContants.PHOTOS_URL, result,
    42. mActivity);
    43. }
    44. @Override
    45. public void onFailure(HttpException error, String msg) {
    46. Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
    47. .show();
    48. error.printStackTrace();
    49. }
    50. });
    51. }
    52. protected void parseData(String result) {
    53. Gson gson = new Gson();
    54. PhotosData data = gson.fromJson(result, PhotosData.class);
    55. mPhotoList = data.data.news;// 获取组图列表集合
    56. if (mPhotoList != null) {
    57. mAdapter = new PhotoAdapter();
    58. lvPhoto.setAdapter(mAdapter);
    59. gvPhoto.setAdapter(mAdapter);
    60. }
    61. }
    62. class PhotoAdapter extends BaseAdapter {
    63. private BitmapUtils utils;
    64. public PhotoAdapter() {
    65. utils = new BitmapUtils(mActivity);
    66. utils.configDefaultLoadingImage(R.drawable.news_pic_default);
    67. }
    68. @Override
    69. public int getCount() {
    70. return mPhotoList.size();
    71. }
    72. @Override
    73. public PhotoInfo getItem(int position) {
    74. return mPhotoList.get(position);
    75. }
    76. @Override
    77. public long getItemId(int position) {
    78. return position;
    79. }
    80. @Override
    81. public View getView(int position, View convertView, ViewGroup parent) {
    82. ViewHolder holder;
    83. if (convertView == null) {
    84. convertView = View.inflate(mActivity, R.layout.list_photo_item,
    85. null);
    86. holder = new ViewHolder();
    87. holder.tvTitle = (TextView) convertView
    88. .findViewById(R.id.tv_title);
    89. holder.ivPic = (ImageView) convertView
    90. .findViewById(R.id.iv_pic);
    91. convertView.setTag(holder);
    92. } else {
    93. holder = (ViewHolder) convertView.getTag();
    94. }
    95. PhotoInfo item = getItem(position);
    96. holder.tvTitle.setText(item.title);
    97. utils.display(holder.ivPic, item.listimage);
    98. return convertView;
    99. }
    100. }
    101. static class ViewHolder {
    102. public TextView tvTitle;
    103. public ImageView ivPic;
    104. }
    105. private boolean isListDisplay = true;// 是否是列表展示
    106. /**
    107. * 切换展现方式
    108. */
    109. private void changeDisplay() {
    110. if (isListDisplay) {
    111. isListDisplay = false;
    112. lvPhoto.setVisibility(View.GONE);
    113. gvPhoto.setVisibility(View.VISIBLE);
    114. btnPhoto.setImageResource(R.drawable.icon_pic_list_type);
    115. } else {
    116. isListDisplay = true;
    117. lvPhoto.setVisibility(View.VISIBLE);
    118. gvPhoto.setVisibility(View.GONE);
    119. btnPhoto.setImageResource(R.drawable.icon_pic_grid_type);
    120. }
    121. }
    122. }
    4.其他
    在basepager里添加这样一个按钮,并findviewbyid出来,因为菜单是新闻中心里才有的,组图是菜单里的一项
    1. <ImageButton
    2. android:id="@+id/btn_photo"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:layout_alignParentRight="true"
    6. android:layout_centerVertical="true"
    7. android:layout_marginRight="5dp"
    8. android:background="@null"
    9. android:src="@drawable/icon_pic_grid_type"
    10. android:visibility="gone" />
    新闻中心页面setCurrentMenuDetailPager
    1. if (pager instanceof PhotoMenuDetailPager) {
    2. btnPhoto.setVisibility(View.VISIBLE);
    3. } else {
    4. btnPhoto.setVisibility(View.GONE);
    5. }










  • 相关阅读:
    浅析匿名内部类
    Neo4j学习实录 下载-安装-springboot操作neo4j
    Objective-C内存管理教程和原理剖析2
    Objective-C内存管理教程和原理剖析
    Objective-C语法快速参考
    Objective-C语法
    一点基础的东西:Objective-C的类型和常量
    IOS开发之----异常处理
    UITextField的详细使用
    TextField知多少
  • 原文地址:https://www.cnblogs.com/sixrain/p/4918959.html
Copyright © 2011-2022 走看看