关于BaseAdapter使用与优化

在activity_main.xml里定义一个ListVIew


新建一个item.xml布局

在这个布局里,我们假设我们的每一个列表项有三个内容,一个缩略图,一个标题,一个概略内容.我们使用RelativeLayout,添加一个ImageView两个Textview



    

    

    

新建一个ItemBeam对像,用于封装数据

public class ItemBean {

    public int ItemImageResid;
    public String ItemTittle;
    public String ItemContent;

    public ItemBean(int itemImageResid, String itemTittle, String itemContent) {
        ItemImageResid = itemImageResid;
        ItemTittle = itemTittle;
        ItemContent = itemContent;
    }
}

新建MyAdapter ,将MyAdapter extends BaseAdapter

在构造方法中初始化用于映射的数据List,重写方法

private List mlist;
    private LayoutInflater mInflater;

    public MyAdapter(Context context,List list){
        mlist = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

创建ViewHolder内部类,创建布局映射关系

class ViewHolder{
        public ImageView imageView;
        public TextView tittle;
        public TextView content;
    }

在getView中判断convertView是否为空

如果为空,则创建convertView,并设置tag,否则通过tag来取出ViewHolder

if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item,null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.tittle = (TextView) convertView.findViewById(R.id.tv_tittle);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

给viewH中的空间设置数据

 ItemBean bean = mlist.get(position);
        viewHolder.imageView.setImageResource(bean.ItemImageResid);
        viewHolder.tittle.setText(bean.ItemTittle);
        viewHolder.content.setText(bean.ItemContent);

完整的MyAdapter

public class MyAdapter extends BaseAdapter{

    private List mlist;
    private LayoutInflater mInflater;

    public MyAdapter(Context context,List list){
        mlist = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
     
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item,null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.tittle = (TextView) convertView.findViewById(R.id.tv_tittle);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ItemBean bean = mlist.get(position);
        viewHolder.imageView.setImageResource(bean.ItemImageResid);
        viewHolder.tittle.setText(bean.ItemTittle);
        viewHolder.content.setText(bean.ItemContent);
        return convertView;
    }
    class ViewHolder{
        public ImageView imageView;
        public TextView tittle;
        public TextView content;
    }
}

MainActivity的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List itemBeanList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            itemBeanList.add(new ItemBean(
                    R.mipmap.ic_launcher,
                    "我是标题"+i,
                    "我是内容"+i
            ));
        }
        ListView listView = (ListView) findViewById(R.id.lv_main);
        listView.setAdapter(new MyAdapter(this,itemBeanList));
    }
}

总结

这样使用BaseAdapter为ListView添加数据充分利用了ListView的缓存机制,使得ListView内容加载时间大大缩减,推荐使用这种方法。

android新手,欢迎指正

你可能感兴趣的:(关于BaseAdapter使用与优化)