如果我快速滚动,GridView 在 android 中将变为空白
请给我一个解决方案。
提前致谢..
这是我的代码
gridView.setAdapter(new ImageAdapter(ChangedDashboard.this, stringarray));
这是我在 gridview 上设置的适配器类
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] menuvalues;
public ImageAdapter(Context context, String[] menuValues) {
this.context = context;
this.menuvalues = menuValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.changedashboard, null);
} else {
gridView = (View) convertView;
}
TextView textView = (TextView) gridView
.findViewById(R.id.textView1);
textView.setText(menuvalues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView1);
String mobile = menuvalues[position];
if (mobile.equals("1")) {
imageView.setImageResource(R.drawable.new1);
} else if (mobile.equals("2")) {
imageView.setImageResource(R.drawable.new2);
} else if (mobile.equals("3")) {
imageView.setImageResource(R.drawable.new3);
}else if (mobile.equals("4")) {
imageView.setImageResource(R.drawable.new4);
} else if (mobile.equals("5")) {
imageView.setImageResource(R.drawable.new5);
}else if (mobile.equals("6")) {
imageView.setImageResource(R.drawable.new6);
}
return gridView;
}
public int getCount() {
return menuvalues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
请给我这个解决方案
更新代码
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] menuvalues;
public ImageAdapter(Context context, String[] menuValues) {
this.context = context;
this.menuvalues = menuValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.changedashboard, null);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) gridView
.findViewById(R.id.textView1);
holder.imageView = (ImageView) gridView
.findViewById(R.id.imageView1);
gridView.setTag(holder);
} else {
gridView = (View) convertView;
}
/*TextView textView = (TextView) gridView
.findViewById(R.id.textView1);*/
// set image based on selected text
/*ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView1);*/
String mobile = menuvalues[position];
ViewHolder holder = (ViewHolder)gridView.getTag();
holder.textView.setText(mobile);
if (mobile.equals("1")) {
holder.imageView.setImageResource(R.drawable.new1);
} else if (mobile.equals("2")) {
holder.imageView.setImageResource(R.drawable.new2);
} else if (mobile.equals("3")) {
holder.imageView.setImageResource(R.drawable.new3);
}else if (mobile.equals("4")) {
holder.imageView.setImageResource(R.drawable.new4);
} else if (mobile.equals("5")) {
holder.imageView.setImageResource(R.drawable.new5);
}
return gridView;
}
public int getCount() {
return menuvalues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder {
TextView textView;
ImageView imageView;
}
}