0

当我使用 GridView 并向 GridView 添加自定义布局时,我看到了这个错误,我的应用程序运行很慢

  02-28 18:39:59.252: I/Choreographer(5586): Skipped 263 frames!  The application may be doing too much work on its main thread.

我的代码:

     public View getView(int position, View convertView, ViewGroup parent) {

        View vi1=convertView;
        if(convertView==null){

            vi1 = inflater.inflate(R.layout.my_layout, null);

            ImageView imv = (ImageView) vi1.findViewById(R.id.Image);
            ImageView imv2 = (ImageView) vi1.findViewById(R.id.imageView2);
            TextView tw1 = (TextView) vi1.findViewById(R.id.lName);
       //     TextView tw2 = (TextView) vi1.findViewById(R.id.County);
         //   TextView tw3 = (TextView) vi1.findViewById(R.id.user);
//          imageLoader.DisplayImage(null, imv);
    //        imageLoader.DisplayImage(null, imv2);
            tw1.setText("AZ");
        }


          return vi1;


    }
4

2 回答 2

1

你的实现getView()效率很低。搜索“ViewHolder”或“RowWrapper”策略进行改进。一个很好的起点是Android 开发网站上的这个资源,但我绝对推荐阅读Romain Guy 的 Google I/O 2010 关于这个主题的演示文稿。如果您愿意的话,也有一段关于他的会议的 YouTube 视频。

您的结果最终应该看起来像这样:

@Override public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.my_layout, parent, false);
        holder = new ViewHolder(); 
        holder.text = (TextView) convertView.findViewById(R.id.lName); 
        holder.img1 = (ImageView) convertView.findViewById(R.id.Image); 
        holder.img2  = (ImageView) convertView.findViewById(R.id.imageView2); 
        convertView.setTag(holder); 
    } else { 
        holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.text.setText("AZ"); 
    holder.img1.setImageBitmap(...); 
    holder.img2.setImageResource(...); 
    return convertView; 
}

static class ViewHolder { 
    TextView text; 
    ImageView img1, img2; 
}
于 2013-02-28T18:18:18.790 回答
0

编写您的 getView 代码时存在问题。每次都会为每一行创建您的视图。请在下面找到使用 getView 方法的正确方法。您需要创建一个 ViewHolder 类。

 static class ViewHolder {
    ImageView img1;
    ImageView img2;
    TextView tvTitle;

}

      @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                ViewHolder holder;

                if (row == null) {
                    holder = new ViewHolder();
                    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                    row = inflater.inflate(R.layout.my_layout, parent, false);
                    holder.play = (Button) row.findViewById(R.id.btn_list_play);
                    holder.img1 = (ImageView) row.findViewById(R.id.img1_id);
                    holder.img2 = (ImageView) row .findViewById(R.id.img2_id);
                    holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle_id);
                    row.setTag(holder);
            }
        else
        {
                        holder = (ViewHolder ) row.getTag();

        }
                    holder.tvTitle.setText("AZ");

                return row;
            }
于 2013-02-28T18:17:21.650 回答