kunyashaw博客主页 关注kunyashaw新博客 关于kunyashaw 转到底部

43、gridview或者listview的adapter优化

1、在getview时,如果是一个textview,那么不用每次都new一个或者inflater直接返回,可以先判断convertview是否为空,如果为空则new或者inflate,否则直接返回convertview

2、如果adapter数量太多,假设需要显示1000个数据,那么如果在getview时每次都是对inflate,那么是一种对资源的浪费,且卡顿现象严重,此时可以通过结合viewHolder提高效率

 

举例:

1、adapter的getview方法

@Override
    public View getView( int pos, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        
            ViewHolderPushHistory holder;
            final int position = pos;
            
            if(convertView == null)
            {
                
                CommonUtils.LogWuwei(tag, "pushHistoryAdapter  getView position is "+pos);
                
                LayoutInflater inflater = (LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                
                holder = new ViewHolderPushHistory();
                
                convertView = inflater.inflate(R.layout.gridview_history_item, null);
                
                holder.tv_serinal_num = (TextView)convertView.findViewById(R.id.textview_grdiview_history_item_serinal_num);
                
                holder.tv_whether_packaged = (TextView)convertView.findViewById(R.id.textview_grdiview_history_item_whether_packaged);
                
                holder.buttonContent = (TextView)convertView.findViewById(R.id.button_gridview_history_item_order_content);
            
                holder.buttonTime = (TextView)convertView.findViewById(R.id.textview_grdiview_history_item_time);
                
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolderPushHistory)convertView.getTag();
            }
            
            
            holder.tv_serinal_num.setText(serinal_num);

            holder.buttonContent.setText("鱼香肉丝");

            holder.tv_whether_packaged.setText("打包");                
            
            holder.buttonTime.setText(“2:12”);
            
            
        
            return convertView;
        
    }

 

2、定义一个存储信息的类

 1 package com.shanfubaoAndroid.kitchenMealDone;
 2 
 3 
 4 import android.widget.TextView;
 5 
 6 public class ViewHolderPushHistory {
 7     
 8     
 9     public TextView tv_serinal_num;
10     
11     public TextView tv_whether_packaged ;
12     
13     public TextView buttonContent ;
14     
15     public TextView buttonTime ;
16     
17 
18 }

 

posted @ 2015-07-09 18:04  kunyashaw  阅读(241)  评论(0编辑  收藏  举报
回到顶部