这是我的GridView的自定义适配器类。我试图通过调用布局inflater一次来优化网格滚动的速度,并且还使用视图持有者,但现在滚动速度非常慢。
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c, String[] web, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
class MyViewHolder {
ImageView imageView;
TextView textView;
MyViewHolder(View v) {
imageView = (ImageView) v.findViewById(R.id.grid_image);
textView = (TextView) v.findViewById(R.id.grid_text);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid = convertView;
MyViewHolder viewHolder;
if (grid == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.grid_single, null);
viewHolder = new MyViewHolder(grid);
grid.setTag(viewHolder);
} else {
grid = convertView;
viewHolder= (MyViewHolder) grid.getTag();
}
viewHolder.textView.setText(web[position]);
viewHolder.imageView.setImageResource(Imageid[position]);
return grid;
}
答案 0 :(得分:1)
我认为你的代码应该可以正常工作。唯一可能的原因是您加载的图像太大,以至于内存无法同时加载所有图像。
您可能想要尝试更小的尺寸。或者您可以考虑使用Glide加载图像,因为它会根据ImageView的大小调整图像大小。
答案 1 :(得分:1)