我有一个从空开始的gridview,然后当用户触摸一个按钮时,他们会在列表中添加一个“练习”,然后我会像这样通知gridview
Exercise e = new Exercise();
w.addExercise(e);
bwGVA.notifyDataSetChanged();
然后出现一个空的网格视图框,如果用户触摸它,则会出现一系列图像,并且它们触摸的图像将成为网格视图中的图像视图。但是,如果他们触摸按钮并向gridview添加另一个练习,那么deafult已经拥有相同的图像。它应该是空白的
但这不是我认为我的问题所在。以下是gridview适配器中的getview
public class BuildWorkoutGridViewAdapter extends BaseAdapter{
private static ArrayList<Exercise> ExerciseList;
Context c;
public BuildWorkoutGridViewAdapter(Context context, ArrayList<Exercise> _list) {
c = context;
ExerciseList = _list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ExerciseList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null){
LayoutInflater li = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.build_exercise_view, null);
}else{
v = (View) convertView;
}
Exercise e = ExerciseList.get(position);
TextView Reps = (TextView) v.findViewById(R.id.tvCustomReps);
TextView Sets = (TextView) v.findViewById(R.id.tvCustomSets);
ImageView ivThumbnail = (ImageView) v
.findViewById(R.id.ivBuildWorkout_Thumbnail);
if (e.getSets() != null) {
Sets.setText(e.getSets().trim());
}
if (e.getReps() != null) {
Reps.setText(e.getReps().trim());
}
if (e.getFilepath() != null) {
System.out.println("filepath:" + e.getFilepath().trim());
BitmapHelper BH = new BitmapHelper(c);
BH.loadSingleBitmap("WorkoutImages/" + e.getFilepath().trim(),
c, ivThumbnail);
e.setImageStatus(true);
}
// TODO Auto-generated method stub
return v;
}
@Override
public Exercise getItem(int position) {
// TODO Auto-generated method stub
return ExerciseList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
我认为这与convertview有关。但我无法弄清楚出了什么问题。
答案 0 :(得分:1)
使用ViewHolder
模式进行平滑滚动和效果
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
static class ViewHolder
{
TextView reps,Sets;
ImageView ivThumbnail;
}
将getView
更改为
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater li = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.build_exercise_view, null);
holder = new ViewHolder();
holder.Reps = (TextView) convertView.findViewById(R.id.tvCustomReps);
holder.Sets = (TextView) convertView.findViewById(R.id.tvCustomSets);
holder.ivThumbnail = (ImageView) convertView.findViewById(R.id.ivBuildWorkout_Thumbnail);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Exercise e = ExerciseList.get(position);
if (e.getSets() != null) {
holder.Sets.setText(e.getSets().trim());
}
if (e.getReps() != null) {
holder.Reps.setText(e.getReps().trim());
}
if (e.getFilepath() != null) {
System.out.println("filepath:" + e.getFilepath().trim());
BitmapHelper BH = new BitmapHelper(c);
BH.loadSingleBitmap("WorkoutImages/" + e.getFilepath().trim(),
c, holder.ivThumbnail);
e.setImageStatus(true);
}
return convertView;
}
如果您需要更新gridView更新基础数据,则填充gridView并调用适配器上的notifyDataSetChanged
。