注意到刚才的几个ViewHolder很多都是类似的,因此ViewHolder也可以抽取出来单独抽到BaseHolder,继承BaseHolder即可
BaseHolder.java
package com.ldw.market.holder;
import android.view.View;
import com.ldw.market.utils.BitmapHelper;
import com.lidroid.xutils.BitmapUtils;
/*
* ViewHolder共性的抽取
*/
public abstract class BaseHolder<Data> {
private View contentView;
private Data data;
protected BitmapUtils bitmapUtils;
//构造方法
public BaseHolder(){
bitmapUtils = BitmapHelper.getBitmapUtils();
contentView=initView();
contentView.setTag(this);
}
/** 创建界面*/
public abstract View initView();
public View getContentView() {
return contentView;
}
public void setData(Data data){
this.data=data;
refreshView(data);
}
/** 根据数据刷新界面*/
public abstract void refreshView(Data data);
}
抽取以后的HomeFragment.java
package com.ldw.market.fragment;
import java.util.List;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
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.http.HttpHelper;
import com.ldw.market.protocol.HomeProtocal;
import com.ldw.market.utils.UiUtils;
import com.ldw.market.utils.BitmapHelper;
import com.ldw.market.view.BaseListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
/*
* Home页面,主体的加载的几个页面和服务器请求数据都是在BaseFragment中去实现
*/
public class HomeFragment extends BaseFragment {
private List<AppInfo> appInfos;
// 当Fragment挂载的activity创建的时候调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
show();
}
public View createSuccessView() {
//使用自定义的ListView,ListView初始化的时候会初始化几个方法
BaseListView listView = new BaseListView(UiUtils.getContext());
listView.setAdapter(new HomeAdapter(appInfos));
// 第二个参数 慢慢滑动的时候是否加载图片 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) {
super(appInfos);
// TODO Auto-generated constructor stub
}
@Override
protected BaseHolder<AppInfo> getHolder() {
// TODO Auto-generated method stub
return new ViewHolder();
}
}
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() {
HomeProtocal protocol = new HomeProtocal();
appInfos = protocol.load(0);
return checkData(appInfos);
}
}
抽取以后的SubjectFragment.java
package com.ldw.market.fragment;
import java.util.List;
import com.ldw.market.R;
import com.ldw.market.Adapter.DefaultAdapter;
import com.ldw.market.domain.SubjectInfo;
import com.ldw.market.fragment.BaseFragment.LoadResult;
import com.ldw.market.holder.BaseHolder;
import com.ldw.market.http.HttpHelper;
import com.ldw.market.protocol.SubjectProtocol;
import com.ldw.market.utils.BitmapHelper;
import com.ldw.market.utils.UiUtils;
import com.lidroid.xutils.BitmapUtils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class SubjectFragment extends BaseFragment {
private List<SubjectInfo> datas;
@Override
public View createSuccessView() {
ListView listView=new ListView(UiUtils.getContext());
listView.setAdapter(new SubjectAdapter(datas));
return listView;
}
private class SubjectAdapter extends DefaultAdapter<SubjectInfo>{
//构造函数,初始化数据
public SubjectAdapter(List<SubjectInfo> datas) {
super(datas);
// TODO Auto-generated constructor stub
}
@Override
protected BaseHolder<SubjectInfo> getHolder() {
return new ViewHolder();
}
}
class ViewHolder extends BaseHolder<SubjectInfo>{
ImageView item_icon;
TextView item_txt;
//更新界面
@Override
public void refreshView(SubjectInfo data){
this.item_txt.setText(data.getDes());
bitmapUtils.display(this.item_icon, HttpHelper.URL+"image?name="+data.getUrl());
}
@Override
public View initView() {
View contentView=View.inflate(UiUtils.getContext(), R.layout.item_subject, null);
this.item_icon=(ImageView) contentView.findViewById(R.id.item_icon);
this.item_txt=(TextView) contentView.findViewById(R.id.item_txt);
return contentView;
}
}
@Override
protected LoadResult load() {
SubjectProtocol protocol=new SubjectProtocol();
datas = protocol.load(0);
return checkData(datas);
}
}
DefaultAdapter.java共性的Adapter
package com.ldw.market.Adapter;
import java.util.List;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.holder.BaseHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/*
* BaseAdapter的共性方法的封装,使用泛型封装
*/
public abstract class DefaultAdapter<Data> extends BaseAdapter{
protected List<Data> datas;
//get方法
public List<Data> getDatas() {
return datas;
}
//set方法
public void setDatas(List<Data> datas) {
this.datas = datas;
}
//构造函数,初始化数据
public DefaultAdapter(List<Data> datas) {
this.datas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//BaseHolder使用泛型以支持多种数据类型的Holder
BaseHolder<Data> holder;
if(convertView == null){
//每一个栏目的holder不一样
holder = getHolder();
}else{
holder = (BaseHolder) convertView.getTag();
}
Data data = datas.get(position);
holder.setData(data);
return holder.getContentView();
}
protected abstract BaseHolder<Data> getHolder();
}
BaseHolder.java共性的ViewHolder,需要注意的是每一个项目的ViewHolder是不一样的,因此让子类去返回Holder,使用抽象的方法getHolder来获取子类的ViewHolder();
package com.ldw.market.Adapter;
import java.util.List;
import com.ldw.market.domain.AppInfo;
import com.ldw.market.holder.BaseHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/*
* BaseAdapter的共性方法的封装,使用泛型封装
*/
public abstract class DefaultAdapter<Data> extends BaseAdapter{
protected List<Data> datas;
//get方法
public List<Data> getDatas() {
return datas;
}
//set方法
public void setDatas(List<Data> datas) {
this.datas = datas;
}
//构造函数,初始化数据
public DefaultAdapter(List<Data> datas) {
this.datas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//BaseHolder使用泛型以支持多种数据类型的Holder
BaseHolder<Data> holder;
if(convertView == null){
//每一个栏目的holder不一样,因此让子类去实现,让每一个子类返回自己的ViewHolder来获取到子类的Holder
holder = getHolder();
}else{
holder = (BaseHolder) convertView.getTag();
}
Data data = datas.get(position);
holder.setData(data);
return holder.getContentView();
}
protected abstract BaseHolder<Data> getHolder();
}