baseadapter使用

作者: 水固态中 | 来源:发表于2018-01-26 03:27 被阅读0次
    public class NewsAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater;
        private List<NewsBean> mNewsList;
        private Context context;//glide加载用
       
        public NewsAdapter(Context context, List<NewsBean> listNewsBean){
            this.mInflater = LayoutInflater.from(context);
            this.mNewsList = listNewsBean;
            this.context=context;
        }
    
        @Override
        public int getCount ( )
        {   //适配器中数据集的数据个数
            return mNewsList==null?0:mNewsList.size();
        }
    
        @Override
        public Object getItem ( int position )
        {   //获取数据集中与所给索引对应的数据项
            return mNewsList.get(position);
        }
    
        @Override
        public long getItemId ( int position )
        {   //获取指定行对应的ID
            return position;
        }
      
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {   //获取每一个Item显示内容view
            ViewHolder viewHolder=null; 
            //实例化view,获取控件id
            if (convertView == null) 
            { 
                //View未实例化,缓存池中无缓存;需要创建;
                //convertView的回收重用机制。
                convertView = mInflater.inflate(R.layout.newslistview_item, null); 
                viewHolder = new ViewHolder(); 
                viewHolder.title = (TextView) convertView.findViewById(R.id.item_title); 
                viewHolder.description = (TextView) convertView.findViewById(R.id.item_des); 
                viewHolder.thumb = (ImageView) convertView.findViewById(R.id.item_thumb);  
                //View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
                convertView.setTag(viewHolder); 
            } 
            else 
            { 
                viewHolder = (ViewHolder) convertView.getTag(); 
            } 
            //设置数据
            NewsBean newsBean =mNewsList.get(position); 
            viewHolder.title.setText(newsBean.getTitle()); 
            viewHolder.description.setText(newsBean.getDescription()); 
            Glide.with(context)
                .load(newsBean.getThumb())
                .into(viewHolder.thumb);
            return convertView;
            
        }
        //避免重复findViewById
        private class ViewHolder { 
            ImageView thumb; 
            TextView title; 
            TextView description;
        }
    }
    

    主活动代码

    
    

    相关文章

      网友评论

        本文标题:baseadapter使用

        本文链接:https://www.haomeiwen.com/subject/fsqiaxtx.html