万能的viewholder

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;
   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值