详情页是有HomeFragment中itemClick事件携带了包名作为参数来实现跳转的,在DetailActivity中实现详情页的布局的逻辑,然后每一个部分都是使用Holder来实现
HomeDragment.java
package com.ldw.market.fragment;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.ldw.market.DetailActivity;
import com.ldw.market.R;
import com.ldw.market.Adapter.DefaultAdapter;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.holder.BaseHolder;
import com.ldw.market.holder.HomePictureHolder;
import com.ldw.market.http.HttpHelper;
import com.ldw.market.protocol.HomeProtocol;
import com.ldw.market.utils.UiUtils;
import com.ldw.market.view.BaseListView;
import com.ldw.market.view.LoadingPage.LoadResult;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
/*
* Home页面,主体的加载的几个页面和服务器请求数据都是在BaseFragment中去实现
*/
public class HomeFragment extends BaseFragment1 {
private List<AppInfo> appInfos;//获取到app的集合
private List<String> pictures;//获取到图片地址的集合
// 当Fragment挂载的activity创建的时候调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
show();
}
public View createSuccessView() {
//使用自定义的ListView,ListView初始化的时候会初始化几个方法
BaseListView listView = new BaseListView(UiUtils.getContext());
//创建一个Holder
HomePictureHolder pictureHolder = new HomePictureHolder();
pictureHolder.setData(pictures);
//获取到holder的view对象
View contentView = pictureHolder.getContentView();
//设置宽和高,方便画图
//contentView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//把holder里面的view添加到ListView的上面
listView.addHeaderView(contentView);
listView.setAdapter(new HomeAdapter(appInfos, listView));
// 第二个参数 慢慢滑动的时候是否加载图片 false 加载 true 不加载
// 第三个参数 飞速滑动的时候是否加载图片 true 不加载
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
bitmapUtils.configDefaultLoadingImage(R.drawable.ic_default); // 设置如果图片加载中显示的图片
bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_default);// 加载失败显示的图片
return listView;
}
private class HomeAdapter extends DefaultAdapter<AppInfo>{
//构造函数,初始化数据
public HomeAdapter(List<AppInfo> appInfos, ListView lv) {
super(appInfos, lv);
// TODO Auto-generated constructor stub
}
@Override
protected BaseHolder<AppInfo> getHolder() {
// TODO Auto-generated method stub
return new ViewHolder();
}
//加载更多数据下拉条的实现
@Override
protected List<AppInfo> onload() {
HomeProtocol protocol = new HomeProtocol();
List<AppInfo> data = protocol.load(appInfos.size());
appInfos.addAll(data);
return data;
}
/**在该方法去处理条目的点击事件*/
@Override
public void onInnerItemClick(int position) {
Toast.makeText(UiUtils.getContext(), "position:"+position, 0).show();
super.onInnerItemClick(position);
AppInfo appInfo = datas.get(position);
//点击以后跳转到详情页
Intent intent = new Intent(UiUtils.getContext(), DetailActivity.class);
//携带参数,点击的是哪一个应用,把包名传递过去
intent.putExtra("packageName", appInfo.getPackageName());
startActivity(intent);
}
}
static class ViewHolder extends BaseHolder<AppInfo>{
ImageView item_icon;
TextView item_title,item_size,item_bottom;
RatingBar item_rating;
//更新界面
@Override
public void refreshView(AppInfo data){
this.item_title.setText(data.getName());// 设置应用程序的名字
String size = Formatter.formatFileSize(UiUtils.getContext(), data.getSize());
this.item_size.setText(size);
this.item_bottom.setText(data.getDes());
float stars = data.getStars();
this.item_rating.setRating(stars); // 设置ratingBar的值
String iconUrl = data.getIconUrl(); //这个地址不全,需要加上apache地址"iconUrl": "app/com.youyuan.yyhl/icon.jpg",
//http://127.0.0.1:8090/image?name=app/com.youyuan.yyhl/icon.jpg
//第一个参数是显示图片的控件,第二个参数是显示图片的url
bitmapUtils.display(this.item_icon, HttpHelper.URL+"image?name="+iconUrl);
// 显示图片的控件
//bitmapUtils.display(holder.item_icon, HttpHelper.URL+"image?name="+iconUrl);
}
@Override
public View initView() {
View contentView = View.inflate(UiUtils.getContext(), R.layout.item_app, null);
this.item_icon = (ImageView) contentView.findViewById(R.id.item_icon);
this.item_title = (TextView) contentView.findViewById(R.id.item_title);
this.item_size = (TextView) contentView.findViewById(R.id.item_size);
this.item_bottom = (TextView) contentView.findViewById(R.id.item_bottom);
this.item_rating = (RatingBar) contentView.findViewById(R.id.item_rating);
return contentView;
}
}
@Override
public LoadResult load() {
HomeProtocol protocol = new HomeProtocol();
//获取到app的信息
appInfos = protocol.load(0);
pictures = protocol.getPictures();
return checkData(appInfos);
}
}
DetailActivity.java
package com.ldw.market;
/*
* 左侧的详情页
*/
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.fragment.BaseFragment1;
import com.ldw.market.holder.DetailDesHolder;
import com.ldw.market.holder.DetailInfoHolder;
import com.ldw.market.holder.DetailSafeHolder;
import com.ldw.market.holder.DetailScreenHolder;
import com.ldw.market.protocol.DetailProtocol;
import com.ldw.market.utils.UiUtils;
import com.ldw.market.view.LoadingPage;
import com.ldw.market.view.LoadingPage.LoadResult;
public class DetailActivity extends BaseActivity{
private String packageName;
private AppInfo data;
@Override
protected void initView() {
System.out.println("3333" + view);
LoadingPage loadingPage = new LoadingPage(this){
@Override
protected LoadResult load() {
return DetailActivity.this.load();
}
@Override
public View createSuccessView() {
return DetailActivity.this.createSuccessView();
}
};
System.out.println("4444" + view);
loadingPage.show();
setContentView(loadingPage);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("1111" + view);
// 获取到打开当前activity的意图对象
Intent intent = getIntent();
//获取到传递过来的包名
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;
private View view;
//加载成功的界面
protected View createSuccessView() {
view = View.inflate(UiUtils.getContext(), R.layout.activity_detail, null);
// 添加信息区域
bottom_layout=(FrameLayout) view.findViewById(R.id.bottom_layout);
// 操作 应用程序信息
detail_info=(FrameLayout) view.findViewById(R.id.detail_info);
detailInfoHolder=new DetailInfoHolder();//创建holder
detailInfoHolder.setData(data);//携带参数,传递到holder,调用refreshView方法
detail_info.addView(detailInfoHolder.getContentView());//holder中初始化布局
System.out.println("555555" + view);
//安全标记
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;
}
//请求服务器加载数据
protected LoadResult load() {
//服务器请求包名的数据
DetailProtocol protocol=new DetailProtocol(packageName);
data = protocol.load(0);
System.out.println("555555data" + data);
if(data == null){
return LoadResult.error;
}else{
return LoadResult.success;
}
}
@Override
protected void initActionBar() {
super.initActionBar();
//获取到actionBar,使用getSupportActionBar,getActionBar是android.app下面的getActionBar
ActionBar actionBar = getSupportActionBar();
//图标的显示与隐藏,显示返回按钮
actionBar.setDisplayHomeAsUpEnabled(true);
}
/*
// 处理actionBar菜单条目的点击事件,实现点击图标来隐藏当前的界面
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("所有");
//当前点击的条目是search
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
*/
}
DetailInfoHolder.java应用的信息的显示
package com.ldw.market.holder;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.ldw.market.R;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.http.HttpHelper;
import com.ldw.market.utils.UiUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
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=View.inflate(UiUtils.getContext(), R.layout.detail_app_info, null);
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.java中间的几张图片的展示可以滚动
package com.ldw.market.holder;
import java.util.List;
import com.ldw.market.R;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.http.HttpHelper;
import com.ldw.market.utils.UiUtils;
import android.view.View;
import android.widget.ImageView;
public class DetailScreenHolder extends BaseHolder<AppInfo> {
private ImageView[] ivs;
@Override
public View initView() {
View view=UiUtils.inflate(R.layout.detail_screen);
//初始化5个图片的集合
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++){
//图片数目小于5的时候
if(i<screen.size()){
ivs[i].setVisibility(View.VISIBLE);
bitmapUtils.display(ivs[i], HttpHelper.URL+"image?name="+screen.get(i));
}else{
//图片数目大于5,让其他的图片不可见
ivs[i].setVisibility(View.GONE);
}
}
}
}