打造万能listView适配器

封装ViewHolder

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

public class ViewHolder {
    private SparseArray mViews;
    private int mPosition;
    private View mConvertView;
    
    public ViewHolder(Context context, ViewGroup parent,int position, int layoutId) {
        this.mPosition = position;
        this.mViews = new SparseArray();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); 
        mConvertView.setTag(this);
    }
    
    public static ViewHolder get(Context context, View convertView, 
            ViewGroup parent,int position, int layoutId) {
        
        if (convertView == null) {
            return new ViewHolder(context, parent, position, layoutId);
        } else {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            return holder;
        }
    }
    
    /**
     * 通过viewId获取控件
     */
    public  T getView(int viewId) {
        
        View view = mViews.get(viewId);
        
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        
        return (T) view;
        
    }

    public View getConvertView() {
        return mConvertView;
    }
    
    /**
     * 封装setText方法,设置TextView的值
     */
    public ViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }
    
    /**
     * 后续可以自定义封装更多的设置图片,网址等的各种方法来完善此ViewHolder
     */
}

编写万能通用适配器CommonAdapter

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class CommonAdapter  extends BaseAdapter{
    
    protected Context mContext;
    protected List mDatas;
    protected LayoutInflater mInflater;
    protected int mLayoutId;
    
    public CommonAdapter (Context context, List datas, int LayoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mLayoutId = LayoutId;
        this.mDatas = datas;
    }

    @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, position, mLayoutId);
        
        convert(holder, getItem(position));
        
        return holder.getConvertView();
    }
    
    public abstract void convert(ViewHolder holder, T t);
}

以上的Viewholder和CommonAdapter都可以在自己项目直接继承使用

使用示例

将自己项目中listview的adapter继承CommonAdapter,以下的weather为一个实体类

import java.util.List;
import com.example.myweather.R;
import com.example.myweather.modle.Weather;
import com.example.myweather.util.CommonAdapter;
import com.example.myweather.util.ViewHolder;
import android.content.Context;

public class SimpleAdapter extends CommonAdapter {

    public SimpleAdapter(Context context, List datas, int LayoutId) {

        super(context, datas, LayoutId);

    }

    @Override
    public void convert(ViewHolder holder, Weather weather) {
        holder.setText(R.id.tvCity, weather.getCity())
                .setText(R.id.tvDate, weather.getDate())
                .setText(R.id.tvTemperatureMax, weather.getTemperatureMax())
                .setText(R.id.tvTemperatureMin, weather.getTemperatureMin())
                .setText(R.id.tvWeather, weather.getWeather());
    }

}

本人以上代码的简易天气查询项目地址:

简易天气预报项目

打造万能listView适配器_第1张图片

你可能感兴趣的:(打造万能listView适配器)