zoukankan      html  css  js  c++  java
  • 9.代码抽取(adapter)


    1  抽取Adapter 共性的方法

    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;
    		}
    		
    	}
    }
    

      



  • 相关阅读:
    ACM/ICPC 之 一道不太简单的DP面试题(Geeksforgeeks)
    ACM/ICPC 之 简单DP-记忆化搜索与递推(POJ1088-滑雪)
    ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)
    ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)
    JAVA手记 JAVA入门(安装+Dos下运行)
    ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
    ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
    ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
    JAVA中的数据结构——集合类(序):枚举器、拷贝、集合类的排序
    JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
  • 原文地址:https://www.cnblogs.com/sixrain/p/4969281.html
Copyright © 2011-2022 走看看