Android:Gridview在快速滚动时变为空白

时间:2012-12-27 10:15:27

标签: android android-layout android-gridview

如果我快速滚动

,那么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;
}

}

2 个答案:

答案 0 :(得分:1)

用户ViewHolder重新使用视图的方法。因为,每次滚动时,都会通过id查找视图,这会进行一些处理并可能导致非常小的延迟,从而导致GridView闪烁。

以下是一些了解ViewHolder的有用链接。

  1. why does the ViewHolder pattren work?
  2. ViewHolder pattern: caching effectively
  3. Commonsware:AdapterViews and Adapters具体参见ViewHolder部分。

答案 1 :(得分:0)

我遇到同样的问题可能是TextView的高度问题。因此,将TextView单行设为:

机器人:SINGLELINE ="真" 或者,如果你想要一些项目的2或3行,那么让所有项目都有2或3行,如:

机器人:的minlines =" 2"

参考:GridView breaks while scrolling