在android gridview中向上滚动不顺畅

时间:2014-12-27 09:15:55

标签: android android-adapter

在我的Android应用程序中,我正在从drawable文件夹加载图像。向下滚动gridview是正常的但是在向上滚动后,图像不对齐。我使用数组适配器和片段

enter image description here

这是我的适配器

public class ProductListAdapter extends ArrayAdapter<ProductInfo> {
Context context;
int layoutResourceId;
List<ProductInfo> data = new ArrayList<ProductInfo>();
public ProductListAdapter(Context context, int layoutResourceId,
        List<ProductInfo> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;
    if (row == null) {      
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new RecordHolder();
        TextView txtTitle = (TextView) row.findViewById(R.id.txtTitle);
        ImageView imageItem = (ImageView) row.findViewById(R.id.imgVItem);
        imageItem.setImageResource(R.drawable.non);
        row.setPadding(0, 0, 0, 10);
        holder.txtTitle = txtTitle;
        holder.imageItem = imageItem;
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }
    ProductInfo _productInfo = data.get(position);
    holder.txtTitle.setText(_productInfo.getProductName().toString());      
    return row;
}
static class RecordHolder {
    TextView txtTitle;
    TextView txtCode;
    ImageView imageItem;
}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;
    if (row == null) {      
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new RecordHolder();
        TextView txtTitle = (TextView) row.findViewById(R.id.txtTitle);
        ImageView imageItem = (ImageView) row.findViewById(R.id.imgVItem);
        imageItem.setImageResource(R.drawable.non);
        row.setPadding(0, 0, 0, 10);
        holder.txtTitle = txtTitle;
        holder.imageItem = imageItem;
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }
    ProductInfo _productInfo = data.get(position);
    holder.txtTitle.setText(_productInfo.getProductName().toString());

//set image background.
holder.imageItem.setImageResource(R.drawable.non);
    return row;
}
static class RecordHolder {
    TextView txtTitle;
    TextView txtCode;
    ImageView imageItem;
}
}