首页 > 代码库 > Android ViewHolder 案例

Android ViewHolder 案例

ViewHolder不是Android的开发API,而是一种设计方法,就是设计个静态类,缓存一下,省得Listview更新的时候,还要重新操作。 
    public View getView(int pos, View convertView, ViewGroup parent){ 

            ViewHolder holder; 

            if (convertView == null) { 

                convertView = mInflater.inflate(R.layout.list_item, null); 

                holder.text = (TextView) convertView.findViewById( R.id.text)); 

                holder.icon = (ImageView) convertView.findViewButId( R.id.icon)); 

                convertView.setTag(holder); 

                } 

            else { 

                holder = (ViewHolder) convertView.getTag(); 

                } 

            holder.text.setText(DATA[pos]); 

            holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2); 

                return convertView; 

//                holder = new ViewHolder(); 

                } 



//     ViewHolder 模式, 效率提高 50% 

        static class ViewHolder { 

            TextView text; 

            ImageView icon; 

            } 

Android ViewHolder 案例