package com.momo2.momo2.adapter;
import java.util.List;
import com.momo2.momo2.MyApplication;
import com.momo2.momo2.R;
import com.momo2.momo2.utils.ScreenUtils;
import com.momo2.momo2.view.MyGridView;
import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class BaskItemGridviewAdapter extends MyBaseAdapter<String> {
public BaskItemGridviewAdapter(List<String> list, Context context) {
super(list, context);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View converview, ViewGroup viewGroup) {
ViewHolder holder;
if (converview == null) {
holder = new ViewHolder();
converview = View.inflate(mContext, R.layout.item_bask_gridview,
null);
holder.imageView = (ImageView) converview
.findViewById(R.id.item_bask_gridview_image);// 图片
converview.setTag(holder);
} else {
holder = (ViewHolder) converview.getTag();
}
MyGridView myGridView = ((MyGridView) viewGroup);
// 获取屏幕大小
int screenWidth = ScreenUtils.getScreenWidth(mContext);
// 设置九宫格里面图片的宽度
int width = (screenWidth - myGridView.getPaddingLeft() - myGridView.getPaddingRight() - (myGridView
.getNumColumns() - 1) * myGridView.getHorizontalSpacing())
/ myGridView.getNumColumns();
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(width,
100));
// String tuString=
// Environment.getExternalStorageDirectory().toString()+"/automobile data recorder/momo20160226_2.jpg";
// 填充图片
/*
* MyApplication.getImageLoader().displayImage( tuString,
* holder.imageView, MyApplication.getFadeOptions(
* R.drawable.empty_photo, R.drawable.empty_photo,
* R.drawable.empty_photo));
*/
// 填充图片
MyApplication.getImageLoader().displayImage(
mList.get(position),
holder.imageView,
MyApplication.getFadeOptions(R.drawable.empty_photo,
R.drawable.empty_photo, R.drawable.empty_photo));
return converview;
}
class ViewHolder {
ImageView imageView;
}
}
使用九宫格的时候最好自定义九宫格:
package com.momo2.momo2.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/**
* 防止显示不全*/
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}