Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之 gridview的Adapter

Android開發之 gridview的Adapter

編輯:關於Android編程

之前寫adapter,getView()中 總是這樣寫:

---------------.......getView(){

View view;

if(convertView == null){

view = View.inflate(this,R.layout.item--;null);

}else{

view=convertView;

}

return view;

}

 

以上寫法也不錯 但是現在看到適配器寫法改變了 返回convertView

特此留下一個模板,方便需要時查看

 

public class SelectiveAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList mGoodsInfos;
    private LayoutInflater mLayoutInflater;
    private int mItemWidth;

    public SelectiveAdapter(Context context, ArrayList goodsInfos){
        mContext = context;
        mGoodsInfos = goodsInfos;
        mLayoutInflater = LayoutInflater.from(context);
        initWidth();
    }
    private void initWidth(){
        int padding = DensityUtil.dip2px(mContext,5);
        int screenWidth = DensityUtil.getScreenWidth((Activity) mContext) - padding * 2;
        mItemWidth =  screenWidth / 3;
    }
    @Override
    public int getCount() {
        return mGoodsInfos != null ? mGoodsInfos.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mGoodsInfos.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_cat_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivGoodAvator = (ImageView) convertView.findViewById(R.id.ivGoodAvator);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GoodsInfo goodsInfo = mGoodsInfos.get(position);
        viewHolder.ivGoodAvator.getLayoutParams().height = (int) (mItemWidth * 2.17);
        Glide.with(mContext).load(goodsInfo.thumb_image).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.img_selective_top1).into(viewHolder.ivGoodAvator);
        return convertView;
    }

    public class ViewHolder {

        @Bind(R.id.ivGoodAvator)
        ImageView ivGoodAvator;

    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved