2 把getView方法里 和holder相关的逻辑 摘取到Holder代码中
3 把Holder 相关的代码 抽取到BaseHolder中
4 把adapter 中getVIew 方法 抽取到了DefaultAdpater中, 其中每个子类getView方法中holder不太一样,所以定义了抽象方法getHolder 要求子类去实现holder
- BaseHolder 所有holder的基类
- DefaultAdapter 所有adapter 的基类
BaseHolder
public abstract class BaseHolder<T> { protected View contentView; protected T data; public BaseHolder(){ contentView=initView(); contentView.setTag(this); // 在这个位置设置的标签 } public void setData(T data) { this.data=data; refreshView(data); } /** * 当Holder中的view对象显示到界面上的时候调用,即getview返回这个 * @return */ public View getContentView() { return contentView; } /** * 当setData调用的时候 会调用该方法 根据数据 显示界面 */ protected abstract void refreshView(T data); /** * 初始化View对象 及其控件 * @return */ protected abstract View initView(); } 子类只需要填充数据和设置控件显示的数据就可以
DefaultAdapter
public abstract class DefaultAdapter<T> extends BaseAdapter { private List<T> datas; public DefaultAdapter(List<T> datas){ this.datas=datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } //convertView 划出屏幕的view对象 @Override public View getView(int position, View convertView, ViewGroup parent) { BaseHolder<T> holder;//和adapter的泛型一样 if(convertView==null){ holder=getHolder(); }else{ holder=(BaseHolder<T>)convertView.getTag(); } T string = datas.get(position); //holder.refreshView(string); holder.setData(string); return holder.getContentView(); } protected abstract BaseHolder<T> getHolder(); }
MainActivity
public class MainActivity extends Activity { private ListView lv; private List<String> datas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datas=new ArrayList<String>(); for(int i=0;i<100;i++){ datas.add("aabbbcc"+i); } lv=(ListView) findViewById(R.id.lv); lv.setAdapter(new MainAdapter(datas)); } public void click(View v){ Intent intent=new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } private class MainAdapter extends DefaultAdapter<String>{ public MainAdapter(List<String> datas) { super(datas); // TODO Auto-generated constructor stub } @Override protected BaseHolder<String> getHolder() { return new MainHolder(); } } class MainHolder extends BaseHolder<String>{ private TextView tv; // 在adapter里list.get(position),然后调用了holer的setdate,而setdata调用了refresh,这样就把data传过来了 @Override protected void refreshView(String data) { this.tv.setText(data); } @Override protected View initView() { View view=View.inflate(getApplicationContext(), R.layout.item_main, null); this.tv=(TextView) view.findViewById(R.id.tv); return view; } } }
SecondActivity
public class SecondActivity extends Activity { private ListView lv_second; private List<Drawable> datas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); lv_second=(ListView) findViewById(R.id.lv_second); datas=new ArrayList<Drawable>(); for(int i=0;i<100;i++){ Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); datas.add(drawable); } lv_second.setAdapter(new SecondAdapter(datas)); } private class SecondAdapter extends DefaultAdapter<Drawable>{ public SecondAdapter(List<Drawable> datas) { super(datas); } @Override protected BaseHolder<Drawable> getHolder() { return new SecondHolder(); } } class SecondHolder extends BaseHolder<Drawable>{ ImageView iv; @Override protected void refreshView(Drawable data) { iv.setImageDrawable(data); } @Override protected View initView() { View view=View.inflate(getApplicationContext(), R.layout.item_second, null); this.iv=(ImageView) view.findViewById(R.id.iv); return view; } } }