7.首页、bitmaputils
HomeProtocol
public class HomeProtocol extends BaseProtocol<List<AppInfo>>{
// 1 把整个json文件写到一个本地文件中 **
// 2 把每条数据都摘出来存到数据库中
// 见到大括号 就用JsonObject ,见到中括号就是JsonArray
public List<AppInfo> paserJson(String json) {
List<AppInfo> appInfos=new ArrayList<AppInfo>();
try {
JSONObject jsonObject=new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("list");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObj = jsonArray.getJSONObject(i);
long id=jsonObj.getLong("id");
String name = jsonObj.getString("name");
String packageName=jsonObj.getString("packageName");
String iconUrl = jsonObj.getString("iconUrl");
float stars=Float.parseFloat(jsonObj.getString("stars"));
long size=jsonObj.getLong("size");
String downloadUrl = jsonObj.getString("downloadUrl");
String des = jsonObj.getString("des");
AppInfo info=new AppInfo(id, name, packageName, iconUrl, stars, size, downloadUrl, des);
appInfos.add(info);
}
return appInfos;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getKey() {
return "home";
}
}
BaseListView (自定义listview)
public class BaseListView extends ListView {
public BaseListView(Context context) {
super(context);
init();
}
public BaseListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BaseListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// setSelector 点击显示的颜色
// setCacheColorHint 拖拽的颜色
// setDivider 每个条目的间隔 的分割线
this.setSelector(R.drawable.nothing); // 什么都没有
this.setCacheColorHint(R.drawable.nothing);
this.setDivider(UiUtils.getDrawalbe(R.drawable.nothing));
}
}
HomeFragment
public class HomeFragment extends BaseFragment {
private List<AppInfo> datas;
// 当Fragment挂载的activity创建的时候调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
show();
}
public View createSuccessView() {
BaseListView listView=new BaseListView(UiUtils.getContext());
listView.setAdapter(new HomeAdapter());
// 第二个参数 慢慢滑动的时候是否加载图片 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 BaseAdapter{
@Override
public int getCount() {
return datas.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if(convertView==null){
view=View.inflate(UiUtils.getContext(), R.layout.item_app, null);
holder=new ViewHolder();
holder.item_icon=(ImageView) view.findViewById(R.id.item_icon);
holder.item_title=(TextView) view.findViewById(R.id.item_title);
holder.item_size=(TextView) view.findViewById(R.id.item_size);
holder.item_bottom=(TextView) view.findViewById(R.id.item_bottom);
holder.item_rating=(RatingBar) view.findViewById(R.id.item_rating);
view.setTag(holder);
}else{
view=convertView;
holder=(ViewHolder) view.getTag();
}
AppInfo appInfo=datas.get(position);
holder.item_title.setText(appInfo.getName());// 设置应用程序的名字
String size=Formatter.formatFileSize(UiUtils.getContext(), appInfo.getSize());
holder.item_size.setText(size);
holder.item_bottom.setText(appInfo.getDes());
float stars = appInfo.getStars();
holder.item_rating.setRating(stars); // 设置ratingBar的值
String iconUrl = appInfo.getIconUrl(); //http://127.0.0.1:8090/image?name=app/com.youyuan.yyhl/icon.jpg
// 显示图片的控件
bitmapUtils.display(holder.item_icon, HttpHelper.URL+"image?name="+iconUrl);
return view;
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
static class ViewHolder{
ImageView item_icon;
TextView item_title,item_size,item_bottom;
RatingBar item_rating;
}
//检测数据
public LoadResult load() {
HomeProtocol protocol=new HomeProtocol();
datas = protocol.load(0);
return checkData(datas);
}
}