11.菜单界面、详情界面
菜单界面
在MainActivity 的initView方法里
// 添加菜单
fl_menu=(FrameLayout) findViewById(R.id.fl_menu);
MenuHolder holder=new MenuHolder();
//登陆的时候数据保存起来了,在到了这里就判断有没有登陆过,这步还没写
//holder.setData(data)
fl_menu.addView(holder.getContentView());
菜单布局(写上这个属性在小屏幕上能滑,大屏幕上是匹配屏幕)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<!--ScollView 包裹的孩子 默认高度只能是包裹内容
android:fillViewport="true"允许孩子是匹配父容器 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
MenuHolder
public class MenuHolder extends BaseHolder<UserInfo> implements OnClickListener {
@ViewInject(R.id.photo_layout)
private RelativeLayout photo_layout;
@ViewInject(R.id.image_photo)
private ImageView image_photo;
@ViewInject(R.id.user_name)
private TextView user_name;
@ViewInject(R.id.user_email)
private TextView user_email;
@Override
public View initView() {
View view=UiUtils.inflate(R.layout.menu_holder);
ViewUtils.inject(this, view);
photo_layout.setOnClickListener(this);
return view;
}
@Override
public void refreshView(UserInfo data) {
user_name.setText(data.getName());
user_email.setText(data.getEmail());
String url = data.getUrl();//image/user.png
bitmapUtils.display(image_photo, HttpHelper.URL+"image?name="+url);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.photo_layout:
// 连接服务器 ...登录
ThreadManager.getInstance().createLongPool().execute(new Runnable() {
@Override
public void run() {
UserProtocol protocol=new UserProtocol();
final UserInfo load = protocol.load(0);
UiUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
setData(load); // 当调用该方法的时候 就会调用refreshView
}
});
}
});
break;
}
}
}
详情界面
解析json:jsonobj解析可以都写在一个bean对象里,它并补关心括号前的字,只需要把属性定义出来
{
"id": 1640273,
"name": "京东",
"packageName": "com.jingdong.app.mall",
"iconUrl": "app/com.jingdong.app.mall/icon.jpg",
"stars": 3,
"downloadNum": "1000万+",
"version": "3.6.3",
"date": "2014-06-13",
"size": 14282026,
"downloadUrl": "app/com.jingdong.app.mall/com.jingdong.app.mall.apk",
"des": "京东是中国最大的综合网购平台, 正品行货, 售后上门取件, 省钱又放心.商品品类覆盖家电,数码通讯,电脑,家居百货,服装服饰,母婴,图书,食品等12大类数万个品牌超百万种优质商品, 是网购用户首选.\n京东Android版是一款基于Android平台的网络购物软件,不仅具有下单,查询订单,搜索商品,晒单,产品评价等常用功能,还实现了手机版特有的“条码购”,“语音充值”,“物流实时跟踪”,“拍照晒单”等特色功能.\n主要功能简介:\n1.足不出户,百万商品任你选。热门促销,天天折扣享不停。\n2.支持语音搜索、语音充值、条码购物,解放你的双手,告别繁琐操作。\n3.商品一日三送,订单全程实时跟踪,一切尽在掌握。\n4.支持京东白条购物,可享受“先消费,后付款”的全新购物体验。更有灵活的分期付款方式可供选择,最长可分24期,享有超低的分期费率。\n5.支持扫码支付,通过京东客户端的“扫一扫”功能,扫描京东订单的二维码或条形码方式后跳转到支付页面进行付款。可以使用快捷,白条、小金库等方式付款。",
"author": "京东商城",
"screen": [
"app/com.jingdong.app.mall/screen0.jpg",
"app/com.jingdong.app.mall/screen1.jpg",
"app/com.jingdong.app.mall/screen2.jpg",
"app/com.jingdong.app.mall/screen3.jpg",
"app/com.jingdong.app.mall/screen4.jpg"
],
"safe": [
{
"safeUrl": "app/com.jingdong.app.mall/safeIcon0.jpg",
"safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl0.jpg",
"safeDes": "已通过安智市场官方认证,是正版软件",
"safeDesColor": 0
},
{
"safeUrl": "app/com.jingdong.app.mall/safeIcon1.jpg",
"safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl1.jpg",
"safeDes": "已通过安智市场安全检测,请放心使用",
"safeDesColor": 0
},
{
"safeUrl": "app/com.jingdong.app.mall/safeIcon2.jpg",
"safeDesUrl": "app/com.jingdong.app.mall/safeDesUrl2.jpg",
"safeDes": "无任何形式的广告",
"safeDesColor": 0
}
]
}
拿以前的APPinfo用,可以根据需求提供的不同构造函数
public class AppInfo {
private long id;
private String name;
private String packageName;
private String iconUrl;
private float stars;
private long size;
private String downloadUrl;
private String des;
//------------- 在DetailActivity 额外用到的数据
private String downloadNum;
private String version;
private String date;
private String author;
private List<String> screen;
private List<String> safeUrl;
private List<String> safeDesUrl;
private List<String> safeDes;
private List<Integer> safeDesColor;
public AppInfo(long id, String name, String packageName, String iconUrl,
float stars, long size, String downloadUrl, String des,
String downloadNum, String version, String date, String author,
List<String> screen, List<String> safeUrl, List<String> safeDesUrl,
List<String> safeDes, List<Integer> safeDesColor) {
....
DetailProtocol
@Override
public AppInfo paserJson(String json) {
try {
JSONObject object = new JSONObject(json);
long id = object.getLong("id");
String name = object.getString("name");
String packageName = object.getString("packageName");
String iconUrl = object.getString("iconUrl");
float stars = Float.parseFloat(object.getString("stars"));
long size = object.getLong("size");
String downloadUrl = object.getString("downloadUrl");
String des = object.getString("des");
String downloadNum = object.getString("downloadNum");
String version = object.getString("version");
String date = object.getString("date");
String author = object.getString("author");
List<String> screen = new ArrayList<String>();
JSONArray screenArray = object.getJSONArray("screen");
for (int i = 0; i < screenArray.length(); i++) {
screen.add(screenArray.getString(i));
}
List<String> safeUrl = new ArrayList<String>();
List<String> safeDesUrl = new ArrayList<String>();
List<String> safeDes = new ArrayList<String>();
List<Integer> safeDesColor = new ArrayList<Integer>();
JSONArray jsonArray = object.getJSONArray("safe");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
safeUrl.add(jsonObject.getString("safeUrl"));
safeDesUrl.add(jsonObject.getString("safeDesUrl"));
safeDes.add(jsonObject.getString("safeDes"));
safeDesColor.add(jsonObject.getInt("safeDesColor"));
}
AppInfo appInfo = new AppInfo(id, name, packageName, iconUrl,
stars, size, downloadUrl, des, downloadNum, version, date,
author, screen, safeUrl, safeDesUrl, safeDes, safeDesColor);
return appInfo;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
DetailActivity
从上到下,每个都是holder,先实现不带箭头的
布局(下面是个帧布局,上面是Scrollview,每像界面是帧布局,直接把每个holder添加进去就行):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@drawable/detail_bottom_bg" >
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_layout"
android:layout_marginBottom="5dp"
android:fillViewport="true" >
public class DetailActivity extends BaseActivity {
private String packageName;
private AppInfo data;
@Override
protected void initView() {
LoadingPage loadingPage=new LoadingPage(this) {
@Override
protected LoadResult load() {
return DetailActivity.this.load();
}
@Override
public View createSuccessView() {
return DetailActivity.this.createSuccessView();
}
};
loadingPage.show(); // 必须调用show方法 才会请求服务器 加载新的界面
setContentView(loadingPage);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent(); // 获取到打开当前activity的意图对象
packageName = intent.getStringExtra("packageName");
super.onCreate(savedInstanceState);
}
private FrameLayout bottom_layout,detail_info,detail_safe,detail_des;
private HorizontalScrollView detail_screen;
private DetailInfoHolder detailInfoHolder;
private DetailScreenHolder screenHolder;
private DetailSafeHolder safeHolder;
private DetailDesHolder desHolder;
/**
* 加载成功的界面
* @return
*/
protected View createSuccessView() {
View view=UiUtils.inflate(R.layout.activity_detail);
// 添加信息区域
bottom_layout=(FrameLayout) view.findViewById(R.id.bottom_layout);
// 应用程序信息
detail_info=(FrameLayout) view.findViewById(R.id.detail_info);
detailInfoHolder=new DetailInfoHolder();
detailInfoHolder.setData(data);
detail_info.addView(detailInfoHolder.getContentView());
//安全标记
detail_safe=(FrameLayout) view.findViewById(R.id.detail_safe);
safeHolder=new DetailSafeHolder();
safeHolder.setData(data);
detail_safe.addView(safeHolder.getContentView());
//描述
detail_des=(FrameLayout) view.findViewById(R.id.detail_des);
desHolder=new DetailDesHolder();
desHolder.setData(data);
detail_des.addView(desHolder.getContentView());
// 中间5张图片
detail_screen=(HorizontalScrollView) view.findViewById(R.id.detail_screen);
screenHolder=new DetailScreenHolder();
screenHolder.setData(data);
detail_screen.addView(screenHolder.getContentView());
return view;
}
/**
* 请求服务器加载数据
* @return
*/
protected LoadResult load() {
DetailProtocol protocol=new DetailProtocol(packageName);
data = protocol.load(0);
if(data==null){
return LoadResult.error;
}else{
return LoadResult.success;
}
}
@Override
protected void initActionBar() {
super.initActionBar();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
DetailInfoHolder应用程序信息
public class DetailInfoHolder extends BaseHolder<AppInfo> {
@ViewInject(R.id.item_icon)
private ImageView item_icon;
@ViewInject(R.id.item_title)
private TextView item_title;
@ViewInject(R.id.item_rating)
private RatingBar item_rating;
@ViewInject(R.id.item_download)
private TextView item_download;
@ViewInject(R.id.item_version)
private TextView item_version;
@ViewInject(R.id.item_date)
private TextView item_date;
@ViewInject(R.id.item_size)
private TextView item_size;
/***
* 实例化控件和布局
*/
@Override
public View initView() {
View view=UiUtils.inflate(R.layout.detail_app_info);
ViewUtils.inject(this, view);
return view;
}
/**
* 给控件设置数据
*/
@Override
public void refreshView(AppInfo data) {
bitmapUtils.display(item_icon, HttpHelper.URL+"image?name="+data.getIconUrl());
item_title.setText(data.getName());
item_rating.setRating(data.getStars());
item_download.setText("下载:"+data.getDownloadNum());
item_version.setText("版本:"+data.getVersion());
item_date.setText("时间:"+data.getDate());
item_size.setText("大小:"+Formatter.formatFileSize(UiUtils.getContext(), data.getSize()));
}
}
DetailScreenHolder (中间的五张图片,布局就是5张图片):
public class DetailScreenHolder extends BaseHolder<AppInfo> {
private ImageView[] ivs;
@Override
public View initView() {
View view=UiUtils.inflate(R.layout.detail_screen);
ivs=new ImageView[5];
ivs[0]=(ImageView) view.findViewById(R.id.screen_1);
ivs[1]=(ImageView) view.findViewById(R.id.screen_2);
ivs[2]=(ImageView) view.findViewById(R.id.screen_3);
ivs[3]=(ImageView) view.findViewById(R.id.screen_4);
ivs[4]=(ImageView) view.findViewById(R.id.screen_5);
return view;
}
@Override
public void refreshView(AppInfo data) {
List<String> screen = data.getScreen(); // 集合的大小有可能小于5
for(int i=0;i<5;i++){
if(i<screen.size()){
ivs[i].setVisibility(View.VISIBLE);
bitmapUtils.display(ivs[i], HttpHelper.URL+"image?name="+screen.get(i));
}else{
ivs[i].setVisibility(View.GONE);
}
}
}
}