Android BaseAdapter

ListView显示与缓存机制:
     只会加载当前屏幕所要显示的数据。显示完成就会被回收到Recycler中。
 
 
 
BaseAdapter 基本结构:
     public int getCount();     // 适配器中数据集中数据的个数
     public Object getIten(int position);     //获取数据集中与指定索引对应的数据项
     public long getItemId(int position);     //获取指定行对应的ID
     public View getView(int position,View convertView,ViewGroup parent);     //获取每一个Item的显示内容
 
 
ViewHolder优化BaseAdapter:
          创建Bean对象,用于封装数据。
          在构造方法中初始化用于映射的数据LIST.
          创建ViewHolder类,创建布局映射关系。
          判断convertView,为空则创建,并设置TAG,否则通过TAG来取出ViewHolder
          给ViewHolder中的控件设置数据。
 
 
public class Adapter extends BaseAdapter{
 
        private List<ItemBean> list ;
        private LayoutInflater inflater ;
       
        public Adapter(List<ItemBean> list,Context context){
              list= this.list ;
               //context要使用当前的Adapter的界面对象 minflater布局装载器对象
               inflater=LayoutInflater. from(context);
       }
       
        @Override
        public int getCount() {
               // TODO Auto-generated method stub
               return list .size();
       }
 
        @Override
        public Object getItem(int position) {
               // TODO Auto-generated method stub
               return list .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) {
              ViewHold viewHold;
               if(convertView==null){
                     viewHold= new ViewHold();
                     convertView= inflater.inflate(R.layout. item, null );
                     viewHold .ima geView=(ImageView) convertView.findViewById(R.id.IVimage );
                     viewHold. title= (TextView) convertView.findViewById(R.id.title );
                     viewHold. content=(TextView) convertView.findViewById(R.id.text );
                     convertView.setTag(viewHold);
              } else{
                     viewHold=(ViewHold) convertView.getTag();
                     
              }
              ItemBean bean= list.get(position);
              viewHold. imageView.setImageResource(bean.getImageid());
              viewHold. title.setText(bean.getTitle());
              viewHold. content.setText(bean.getText());
               return convertView;
       }
       
       
        class ViewHold{
               public ImageView imageView ;
               public TextView title ;
               public TextView content ;
       }
}
posted @ 2015-09-28 15:48  如梦真心  阅读(312)  评论(0编辑  收藏  举报