ListView优化-通用CommonAdapter编写备份

【ps:CommonAdapter基于ViewHolder工具类】

CommonAdapter.java

package cn.edu.bzu.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.edu.bzu.listview_demo.R;

/**
 * CommonAdapter makes Common extends BaseAdapter
 * Created by monster on 2015/5/3.
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    public CommonAdapter(Context context,List<T>datas){
        this.mContext=context;
        this.mDatas=datas;
        mInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder=ViewHolder.get(mContext,convertView,parent, R.layout.listview_item,position);
        convert(holder,getItem(position));
        return holder.getConvertView();
    }
    public abstract void convert(ViewHolder holder,T t);
}

MyAdapterWithCommonViewHolder.java

package cn.edu.bzu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.edu.bzu.entity.Bean;
import cn.edu.bzu.listview_demo.R;
import cn.edu.bzu.util.CommonAdapter;
import cn.edu.bzu.util.ViewHolder;

/**
 * Created by monster on 2015/4/19.
 * extends CommonAdapter
 */
public class MyAdapterWithCommonViewHolder extends CommonAdapter<Bean> {
//    private LayoutInflater mInflater; //LayoutInflater是用来找layout下xml布局文件,并且实例化
//    private List<Bean> mlist;
//    private Context mcontext;
    public MyAdapterWithCommonViewHolder(Context context, List<Bean> mDatas) {
        super(context,mDatas);
    }

    @Override
    public void convert(ViewHolder holder, Bean bean) {
        ((TextView)holder.getView(R.id.id_title)).setText(bean.getTitle());
        ((TextView)holder.getView(R.id.id_desc)).setText(bean.getDesc());
        ((TextView)holder.getView(R.id.id_time)).setText(bean.getDate());
        ((TextView)holder.getView(R.id.id_phone)).setText(bean.getPhone());
    }

//    @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 holder=ViewHolder.get(mContext,convertView,parent,R.layout.listview_item,position);
//        ((TextView)holder.getView(R.id.id_title)).setText(mDatas.get(position).getTitle());
//        ((TextView)holder.getView(R.id.id_desc)).setText(mDatas.get(position).getDesc());
//        ((TextView)holder.getView(R.id.id_time)).setText(mDatas.get(position).getDate());
//        ((TextView)holder.getView(R.id.id_phone)).setText(mDatas.get(position).getPhone());
//        return holder.getConvertView();
//    }

}

源代码下载链接:https://github.com/monsterLin/CommonAdapter_Package

posted @ 2015-05-03 11:42  monsterLin  阅读(378)  评论(0编辑  收藏  举报
小弟博客地址迁移,欢迎戳这里查看新博客~