import android.util.SparseArray; import android.view.View; /** * @Description:万能的viewHolder * @author http://blog.csdn.net/finddreams */ public class BaseViewHolder { @SuppressWarnings("unchecked") public static <T extends View> T get(View view, int id) { SparseArray<View> viewHolder = (SparseArray<View>) view.getTag(); if (viewHolder == null) { viewHolder = new SparseArray<View>(); view.setTag(viewHolder); } View childView = viewHolder.get(id); if (childView == null) { childView = view.findViewById(id); viewHolder.put(id, childView); } return (T) childView; } }
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; /** * @Description:gridview的Adapter * @author http://blog.csdn.net/finddreams */ public class MyGridAdapter extends BaseAdapter { private Context mContext; public String[] img_text = { "转账", "余额宝", "手机充值", "信用卡还款", "淘宝电影", "彩票", "当面付", "亲密付", "机票", }; public int[] imgs = { R.drawable.app_transfer, R.drawable.app_fund, R.drawable.app_phonecharge, R.drawable.app_creditcard, R.drawable.app_movie, R.drawable.app_lottery, R.drawable.app_facepay, R.drawable.app_close, R.drawable.app_plane }; public MyGridAdapter(Context mContext) { super(); this.mContext = mContext; } @Override public int getCount() { // TODO Auto-generated method stub return img_text.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.grid_item, parent, false); } TextView tv = BaseViewHolder.get(convertView, R.id.tv_item); ImageView iv = BaseViewHolder.get(convertView, R.id.iv_item); iv.setBackgroundResource(imgs[position]); tv.setText(img_text[position]); return convertView; } }