11.菜单界面、详情界面

菜单界面
在MainActivity  的initView方法里
  1. // 添加菜单
  2. fl_menu=(FrameLayout) findViewById(R.id.fl_menu);
  3. MenuHolder holder=new MenuHolder();
  4. //登陆的时候数据保存起来了,在到了这里就判断有没有登陆过,这步还没写
  5. //holder.setData(data)
  6. fl_menu.addView(holder.getContentView());
菜单布局(写上这个属性在小屏幕上能滑,大屏幕上是匹配屏幕)
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:fillViewport="true" >
  5. <!--ScollView 包裹的孩子 默认高度只能是包裹内容
  6. android:fillViewport="true"允许孩子是匹配父容器 -->
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical" >
MenuHolder 
  1. public class MenuHolder extends BaseHolder<UserInfo> implements OnClickListener {
  2. @ViewInject(R.id.photo_layout)
  3. private RelativeLayout photo_layout;
  4. @ViewInject(R.id.image_photo)
  5. private ImageView image_photo;
  6. @ViewInject(R.id.user_name)
  7. private TextView user_name;
  8. @ViewInject(R.id.user_email)
  9. private TextView user_email;
  10. @Override
  11. public View initView() {
  12. View view=UiUtils.inflate(R.layout.menu_holder);
  13. ViewUtils.inject(this, view);
  14. photo_layout.setOnClickListener(this);
  15. return view;
  16. }
  17. @Override
  18. public void refreshView(UserInfo data) {
  19. user_name.setText(data.getName());
  20. user_email.setText(data.getEmail());
  21. String url = data.getUrl();//image/user.png
  22. bitmapUtils.display(image_photo, HttpHelper.URL+"image?name="+url);
  23. }
  24. @Override
  25. public void onClick(View v) {
  26. switch (v.getId()) {
  27. case R.id.photo_layout:
  28. // 连接服务器 ...登录
  29. ThreadManager.getInstance().createLongPool().execute(new Runnable() {
  30. @Override
  31. public void run() {
  32. UserProtocol protocol=new UserProtocol();
  33. final UserInfo load = protocol.load(0);
  34. UiUtils.runOnUiThread(new Runnable() {
  35. @Override
  36. public void run() {
  37. setData(load); // 当调用该方法的时候 就会调用refreshView
  38. }
  39. });
  40. }
  41. });
  42. break;
  43. }
  44. }
  45. }

详情界面
 解析json:jsonobj解析可以都写在一个bean对象里,它并补关心括号前的字,只需要把属性定义出来
  1. {
  2. "id": 1640273,
  3. "name": "京东",
  4. "packageName": "com.jingdong.app.mall",
  5. "iconUrl": "app/com.jingdong.app.mall/icon.jpg",
  6. "stars": 3,
  7. "downloadNum": "1000万+",
  8. "version": "3.6.3",
  9. "date": "2014-06-13",
  10. "size": 14282026,
  11. "downloadUrl": "app/com.jingdong.app.mall/com.jingdong.app.mall.apk",
  12. "des": "京东是中国最大的综合网购平台, 正品行货, 售后上门取件, 省钱又放心.商品品类覆盖家电,数码通讯,电脑,家居百货,服装服饰,母婴,图书,食品等12大类数万个品牌超百万种优质商品, 是网购用户首选.\n京东Android版是一款基于Android平台的网络购物软件,不仅具有下单,查询订单,搜索商品,晒单,产品评价等常用功能,还实现了手机版特有的“条码购”,“语音充值”,“物流实时跟踪”,“拍照晒单”等特色功能.\n主要功能简介:\n1.足不出户,百万商品任你选。热门促销,天天折扣享不停。\n2.支持语音搜索、语音充值、条码购物,解放你的双手,告别繁琐操作。\n3.商品一日三送,订单全程实时跟踪,一切尽在掌握。\n4.支持京东白条购物,可享受“先消费,后付款”的全新购物体验。更有灵活的分期付款方式可供选择,最长可分24期,享有超低的分期费率。\n5.支持扫码支付,通过京东客户端的“扫一扫”功能,扫描京东订单的二维码或条形码方式后跳转到支付页面进行付款。可以使用快捷,白条、小金库等方式付款。",
  13. "author": "京东商城",
  14. "screen": [
  15. "app/com.jingdong.app.mall/screen0.jpg",
  16. "app/com.jingdong.app.mall/screen1.jpg",
  17. "app/com.jingdong.app.mall/screen2.jpg",
  18. "app/com.jingdong.app.mall/screen3.jpg",
  19. "app/com.jingdong.app.mall/screen4.jpg"
  20. ],
  21. "safe": [
  22. {
  23. "safeUrl": "app/com.jingdong.app.mall/safeIcon0.jpg",
  24. "safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl0.jpg",
  25. "safeDes": "已通过安智市场官方认证,是正版软件",
  26. "safeDesColor": 0
  27. },
  28. {
  29. "safeUrl": "app/com.jingdong.app.mall/safeIcon1.jpg",
  30. "safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl1.jpg",
  31. "safeDes": "已通过安智市场安全检测,请放心使用",
  32. "safeDesColor": 0
  33. },
  34. {
  35. "safeUrl": "app/com.jingdong.app.mall/safeIcon2.jpg",
  36. "safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl2.jpg",
  37. "safeDes": "无任何形式的广告",
  38. "safeDesColor": 0
  39. }
  40. ]
  41. }
拿以前的APPinfo用,可以根据需求提供的不同构造函数
  1. public class AppInfo {
  2. private long id;
  3. private String name;
  4. private String packageName;
  5. private String iconUrl;
  6. private float stars;
  7. private long size;
  8. private String downloadUrl;
  9. private String des;
  10. //------------- 在DetailActivity 额外用到的数据
  11. private String downloadNum;
  12. private String version;
  13. private String date;
  14. private String author;
  15. private List<String> screen;
  16. private List<String> safeUrl;
  17. private List<String> safeDesUrl;
  18. private List<String> safeDes;
  19. private List<Integer> safeDesColor;
  20. public AppInfo(long id, String name, String packageName, String iconUrl,
  21. float stars, long size, String downloadUrl, String des,
  22. String downloadNum, String version, String date, String author,
  23. List<String> screen, List<String> safeUrl, List<String> safeDesUrl,
  24. List<String> safeDes, List<Integer> safeDesColor) {
  25. ....
DetailProtocol 
  1. @Override
  2. public AppInfo paserJson(String json) {
  3. try {
  4. JSONObject object = new JSONObject(json);
  5. long id = object.getLong("id");
  6. String name = object.getString("name");
  7. String packageName = object.getString("packageName");
  8. String iconUrl = object.getString("iconUrl");
  9. float stars = Float.parseFloat(object.getString("stars"));
  10. long size = object.getLong("size");
  11. String downloadUrl = object.getString("downloadUrl");
  12. String des = object.getString("des");
  13. String downloadNum = object.getString("downloadNum");
  14. String version = object.getString("version");
  15. String date = object.getString("date");
  16. String author = object.getString("author");
  17. List<String> screen = new ArrayList<String>();
  18. JSONArray screenArray = object.getJSONArray("screen");
  19. for (int i = 0; i < screenArray.length(); i++) {
  20. screen.add(screenArray.getString(i));
  21. }
  22. List<String> safeUrl = new ArrayList<String>();
  23. List<String> safeDesUrl = new ArrayList<String>();
  24. List<String> safeDes = new ArrayList<String>();
  25. List<Integer> safeDesColor = new ArrayList<Integer>();
  26. JSONArray jsonArray = object.getJSONArray("safe");
  27. for (int i = 0; i < jsonArray.length(); i++) {
  28. JSONObject jsonObject = jsonArray.getJSONObject(i);
  29. safeUrl.add(jsonObject.getString("safeUrl"));
  30. safeDesUrl.add(jsonObject.getString("safeDesUrl"));
  31. safeDes.add(jsonObject.getString("safeDes"));
  32. safeDesColor.add(jsonObject.getInt("safeDesColor"));
  33. }
  34. AppInfo appInfo = new AppInfo(id, name, packageName, iconUrl,
  35. stars, size, downloadUrl, des, downloadNum, version, date,
  36. author, screen, safeUrl, safeDesUrl, safeDes, safeDesColor);
  37. return appInfo;
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. return null;
  41. }
  42. }
DetailActivity  
从上到下,每个都是holder,先实现不带箭头的

布局(下面是个帧布局,上面是Scrollview,每像界面是帧布局,直接把每个holder添加进去就行):
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" >
  4. <FrameLayout
  5. android:id="@+id/bottom_layout"
  6. android:layout_width="match_parent"
  7. android:layout_height="50dp"
  8. android:layout_alignParentBottom="true"
  9. android:background="@drawable/detail_bottom_bg" >
  10. </FrameLayout>
  11. <ScrollView
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_above="@id/bottom_layout"
  15. android:layout_marginBottom="5dp"
  16. android:fillViewport="true" >

  1. public class DetailActivity extends BaseActivity {
  2. private String packageName;
  3. private AppInfo data;
  4. @Override
  5. protected void initView() {
  6. LoadingPage loadingPage=new LoadingPage(this) {
  7. @Override
  8. protected LoadResult load() {
  9. return DetailActivity.this.load();
  10. }
  11. @Override
  12. public View createSuccessView() {
  13. return DetailActivity.this.createSuccessView();
  14. }
  15. };
  16. loadingPage.show(); // 必须调用show方法 才会请求服务器 加载新的界面
  17. setContentView(loadingPage);
  18. }
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. Intent intent = getIntent(); // 获取到打开当前activity的意图对象
  22. packageName = intent.getStringExtra("packageName");
  23. super.onCreate(savedInstanceState);
  24. }
  25. private FrameLayout bottom_layout,detail_info,detail_safe,detail_des;
  26. private HorizontalScrollView detail_screen;
  27. private DetailInfoHolder detailInfoHolder;
  28. private DetailScreenHolder screenHolder;
  29. private DetailSafeHolder safeHolder;
  30. private DetailDesHolder desHolder;
  31. /**
  32. * 加载成功的界面
  33. * @return
  34. */
  35. protected View createSuccessView() {
  36. View view=UiUtils.inflate(R.layout.activity_detail);
  37. // 添加信息区域
  38. bottom_layout=(FrameLayout) view.findViewById(R.id.bottom_layout);
  39. // 应用程序信息
  40. detail_info=(FrameLayout) view.findViewById(R.id.detail_info);
  41. detailInfoHolder=new DetailInfoHolder();
  42. detailInfoHolder.setData(data);
  43. detail_info.addView(detailInfoHolder.getContentView());
  44. //安全标记
  45. detail_safe=(FrameLayout) view.findViewById(R.id.detail_safe);
  46. safeHolder=new DetailSafeHolder();
  47. safeHolder.setData(data);
  48. detail_safe.addView(safeHolder.getContentView());
  49. //描述
  50. detail_des=(FrameLayout) view.findViewById(R.id.detail_des);
  51. desHolder=new DetailDesHolder();
  52. desHolder.setData(data);
  53. detail_des.addView(desHolder.getContentView());
  54. // 中间5张图片
  55. detail_screen=(HorizontalScrollView) view.findViewById(R.id.detail_screen);
  56. screenHolder=new DetailScreenHolder();
  57. screenHolder.setData(data);
  58. detail_screen.addView(screenHolder.getContentView());
  59. return view;
  60. }
  61. /**
  62. * 请求服务器加载数据
  63. * @return
  64. */
  65. protected LoadResult load() {
  66. DetailProtocol protocol=new DetailProtocol(packageName);
  67. data = protocol.load(0);
  68. if(data==null){
  69. return LoadResult.error;
  70. }else{
  71. return LoadResult.success;
  72. }
  73. }
  74. @Override
  75. protected void initActionBar() {
  76. super.initActionBar();
  77. ActionBar actionBar = getSupportActionBar();
  78. actionBar.setDisplayHomeAsUpEnabled(true);
  79. }
  80. }
DetailInfoHolder应用程序信息
  1. public class DetailInfoHolder extends BaseHolder<AppInfo> {
  2. @ViewInject(R.id.item_icon)
  3. private ImageView item_icon;
  4. @ViewInject(R.id.item_title)
  5. private TextView item_title;
  6. @ViewInject(R.id.item_rating)
  7. private RatingBar item_rating;
  8. @ViewInject(R.id.item_download)
  9. private TextView item_download;
  10. @ViewInject(R.id.item_version)
  11. private TextView item_version;
  12. @ViewInject(R.id.item_date)
  13. private TextView item_date;
  14. @ViewInject(R.id.item_size)
  15. private TextView item_size;
  16. /***
  17. * 实例化控件和布局
  18. */
  19. @Override
  20. public View initView() {
  21. View view=UiUtils.inflate(R.layout.detail_app_info);
  22. ViewUtils.inject(this, view);
  23. return view;
  24. }
  25. /**
  26. * 给控件设置数据
  27. */
  28. @Override
  29. public void refreshView(AppInfo data) {
  30. bitmapUtils.display(item_icon, HttpHelper.URL+"image?name="+data.getIconUrl());
  31. item_title.setText(data.getName());
  32. item_rating.setRating(data.getStars());
  33. item_download.setText("下载:"+data.getDownloadNum());
  34. item_version.setText("版本:"+data.getVersion());
  35. item_date.setText("时间:"+data.getDate());
  36. item_size.setText("大小:"+Formatter.formatFileSize(UiUtils.getContext(), data.getSize()));
  37. }
  38. }
DetailScreenHolder (中间的五张图片,布局就是5张图片):
  1. public class DetailScreenHolder extends BaseHolder<AppInfo> {
  2. private ImageView[] ivs;
  3. @Override
  4. public View initView() {
  5. View view=UiUtils.inflate(R.layout.detail_screen);
  6. ivs=new ImageView[5];
  7. ivs[0]=(ImageView) view.findViewById(R.id.screen_1);
  8. ivs[1]=(ImageView) view.findViewById(R.id.screen_2);
  9. ivs[2]=(ImageView) view.findViewById(R.id.screen_3);
  10. ivs[3]=(ImageView) view.findViewById(R.id.screen_4);
  11. ivs[4]=(ImageView) view.findViewById(R.id.screen_5);
  12. return view;
  13. }
  14. @Override
  15. public void refreshView(AppInfo data) {
  16. List<String> screen = data.getScreen(); // 集合的大小有可能小于5
  17. for(int i=0;i<5;i++){
  18. if(i<screen.size()){
  19. ivs[i].setVisibility(View.VISIBLE);
  20. bitmapUtils.display(ivs[i], HttpHelper.URL+"image?name="+screen.get(i));
  21. }else{
  22. ivs[i].setVisibility(View.GONE);
  23. }
  24. }
  25. }
  26. }

 




posted @ 2015-11-17 19:23  梦和远方  阅读(388)  评论(0编辑  收藏  举报