1

在 myadapter.java 我有以下代码:

public View getView(int position,View convertView,ViewGroup parent) {
  View view=null;
  if(convertView!=null)view=convertView;else view=newView(context,parent);
  HashMap<String,String> d=new HashMap<String,String>();
  d=data.get(position);
  String _r=d.get("r");
  String out=d.get("out");
  Typeface mf=Typeface.createFromAsset(context.getAssets(),"fonts/mf.ttf");
  TextView txt=(TextView)view.findViewById(R.id.c_n);
  txt.setText(_r);
  txt.setTypeface(mf);
  if(out.equals("yes") && !d.get("sid").equals("-1")) {
    ImageView imag=(ImageView)view.findViewById(R.id.myimage);
    imag.setVisibility(imag.VISIBLE);//This fires sometimes while scroll, while
    //I scroll & where I don't need it.
    //view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.c_c));
    //^ same as setVisibility.
  }
  ...
  return view;
}

当我启动我的应用程序时,这个列表就可以了。但是,当我滚动时,imag.setVisibility(imag.VISIBLE);有时会在我不需要它的地方触发,比如 listview 会生成每个滚动事件。一些 ImageViews 变得可见,而不是在应用程序启动时。

我该如何解决?

4

1 回答 1

1

问题是由convertView它用于重新循环现有视图的方式引起的。

示例 - 假设您的列表适配器有 20 个项目,但ListView屏幕上只能显示 5 个。这 5 个列表项“视图”将通过在滚动convertView时作为参数传递来重新循环。ListView

一旦您设置了 的可见性ImageView,它将保持在 中的设置convertView。换句话说,您需要将其设置为,INVISIBLE或者GONE如果您不希望它可见...

ImageView imag=(ImageView)view.findViewById(R.id.myimage);
if (d.get("ms").equals("yes") && !d.get("sid").equals("-1")) {
    imag.setVisibility(View.VISIBLE);
}
else
    imag.setVisibility(View.INVISIBLE); // Or use View.GONE depending on what you need
于 2012-05-31T18:53:35.117 回答