安卓万能适配器

package com.zhongzhi.grid.adapter;

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


import com.zhongzhi.grid.R;

import java.util.ArrayList;
import java.util.List;


public abstract class AAComAdapter<T> extends BaseAdapter {

	protected Context mContext;
	public List<T> mDatas = new ArrayList<T>();
	protected LayoutInflater mInflater;
	private int mlayoutId;
	private AAViewHolder holder;
	private int topSize = -1;
	private SparseArray<String> arraycolor;
	private boolean showFresh = false;
	private boolean showNoData = true;
	public int nextpage = 1;
	private int selectIndex = -1;
	private ImageView nullImage;
    private int resource;
	public int getSelectIndex() {
		return selectIndex;
	}

	public void setSelectIndex(int selectIndex) {
		this.selectIndex = selectIndex;
	}

	public void setNotShowNoData() {
		showNoData = false;
	}

	public AAComAdapter(Context context, int layoutId, List<T> datas, boolean showFresh) {
		this.mContext = context;
		this.mlayoutId = layoutId;
		mInflater = LayoutInflater.from(context);
		this.mDatas.clear();
		this.mDatas.addAll(datas);
		this.showFresh = showFresh;
	}

	public AAComAdapter(Context context, int layoutId, List<T> datas) {
		this.mContext = context;
		this.mlayoutId = layoutId;
		mInflater = LayoutInflater.from(context);
		this.mDatas.clear();
		this.mDatas.addAll(datas);
		if (this.mDatas.size() == 0) {
			this.mDatas.add(null);
		}
	}

	public AAComAdapter(Context context, int layoutId) {
		this.mContext = context;
		this.mlayoutId = layoutId;
		mInflater = LayoutInflater.from(context);
		this.mDatas.clear();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (mDatas.get(position) == null && mDatas.size() == 1) {
			if(showNoData) {
				View view = LayoutInflater.from(mContext).inflate(R.layout.sl_list_nodata, parent, false);
				nullImage = (ImageView) view.findViewById(R.id.nodata_iv);
				if (resource>0){
					nullImage.setImageResource(resource);
				}
				return view;
			}else
				return LayoutInflater.from(mContext).inflate(R.layout.null_layout, parent, false);
		} else {

			holder = AAViewHolder.get(mContext, convertView, parent, mlayoutId, position, showFresh);
			convert(holder, (T) getItem(position));
			return holder.getConvertView();
		}

	}

	public void addTextColor(int position, String color) {
		if (arraycolor == null) {
			arraycolor = new SparseArray<String>();
		}
		arraycolor.put(position, color);
	}

	public String getTextColor(int position) {
		return arraycolor.get(position);
	}

	public void setNoDataPic(int resource){
		this.resource = resource;
	}

	public abstract void convert(AAViewHolder holder, T mt);

	public AAViewHolder getHolder() {
		return holder;
	}

	public void resetData(List<T> mDatas) {
		this.mDatas.clear();
		this.mDatas.addAll(mDatas);
		if (showNoData) {
			if (this.mDatas.size() == 0) {
				this.mDatas.add(null);
			}
		}
//		notifyDataSetChanged();
	}

	public void addData(List<T> mDatas) {
		this.mDatas = mDatas;
//		notifyDataSetChanged();
	}

	public void setSize(int size){
		this.topSize = size;
	}
	@Override
	public int getCount() {
		int size = mDatas.size();
		if (topSize !=-1 && topSize < size)
			return topSize;
		else
			return mDatas.size();
	}

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

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值