美文网首页
ListView的优化

ListView的优化

作者: 梦沉薇露 | 来源:发表于2016-04-12 10:19 被阅读0次

    ListView是比较常用的组件,基本用法相信大部分同志们都已经非常熟悉熟练的使用了,接下来我来介绍一下简单的优化。

    一、convertView结合ViewHolder

    首先我们要在自定义adaper中定义一个内部类,代码如下:

    //布局的控件资源  
    class viewHolder{  
        CheckBox checkBox;  
        TextView textView;  
    }  
    

    接下来只要在getView()方法中通过视图缓存机制来重用以缓存,完整的示例代码如下:

    class MyAdapter extends BaseAdapter{  
        Context mContext;  
        private List<String> mData;
        private  LayoutInflater inflater;  
      
        public MyAdapter(Context context,List<String> data) {  
            mContext = context;  
            this.mData=data;
            inflater = LayoutInflater.from(mContext);  
        }  
      
        @Override  
        public int getCount() {  
            return mData.size();  
        }  
      
    
        @Override  
        public Object getItem(int arg0) {  
            return mData.get(arg0);  
        }  
      
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
      
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            viewHolder1 holder = null;  
            if(convertView == null)  {   
                    convertView = inflater.inflate(R.layout.listitem2, parent, false);
                    holder = new viewHolder();  
                    holder.textView = (TextView)convertView.findViewById(R.id.textview);  
                    holder.imageView = (ImageView)convertView.findViewById(R.id.imageview);  
                    convertView.setTag(holder);  
                    break;  
               
            }  
            else  {            
                    holder = (viewHolder) convertView.getTag();       
            }  
            holder.textView.setText(Integer.toString(position));  
            holder.imageView.setBackgroundResource(R.drawable.icon); 
            return convertView;  
          
    }  
    
    class viewHolder{  
        ImageView imageView;  
        TextView textView;  
    }  
    

    这样一来 就避免了反复创建大量view的问题了

    但是上面的仍然可以再优化。当我们的ListView中填充的item有多种形式时,比如微信聊天中你和好友的聊天记录明显是两种布局,我们需要用到2种item的布局方式
    此时如果只是单纯判断convert是否存在 会造成回收的view不符合你当前需要的布局
    这里要提到Adapter中的另外2个方法:
    public int getItemViewType(int position) {}
    public int getViewTypeCount() {}
    下面附上一个代码示例:

    class MyAdapter extends BaseAdapter{
        private Context mContext;
        private LayoutInflater inflater;
            private List<String> mData;
        TextView tex;
        final int VIEW_TYPE = 2;
        final int TYPE_1 = 0;
        final int TYPE_2 = 1;
    
        public MyAdapter(Context context) {
            mContext = context;
            inflater = LayoutInflater.from(mContext);
        }
    
        @Override
        public int getCount() {
            return mData.size();
        }
    
        //每个convert view都会调用此方法,获得当前所需要的view样式
        @Override
        public int getItemViewType(int position) {
            int p = position%2;
                    String str=mData.get(position);
            if(p == 0)
                return TYPE_1;
            else if
                return TYPE_2;
            
        }
    
        @Override
        public int getViewTypeCount() {
            return 2;
        }
    
        @Override
        public Object getItem(int arg0) {
            return mData.get(arg0);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            viewHolder1 holder1 = null;
            viewHolder2 holder2 = null;
            int type = getItemViewType(position);
    
            //无convertView,需要new出各个控件
            if(convertView == null)
            { 
                //按当前所需的样式,确定new的布局
                switch(type)
                {
                    case TYPE_1:
                    convertView = inflater.inflate(R.layout.listitem1, parent, false);
                    holder1 = new viewHolder1();
                    holder1.textView = (TextView)convertView.findViewById(R.id.textview1);
                    holder1.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
                    convertView.setTag(holder1);
                    break;
                    case TYPE_2:
                    convertView = inflater.inflate(R.layout.listitem2, parent, false);
                    holder2 = new viewHolder2();
                    holder2.textView = (TextView)convertView.findViewById(R.id.textview2);
                    holder2.imageView = (ImageView)convertView.findViewById(R.id.imageview);
                    convertView.setTag(holder2);
                    break;
                }
            }
            else
            {
                //有convertView,按样式,取得不用的布局
                switch(type)
                {
                    case TYPE_1:
                    holder1 = (viewHolder1) convertView.getTag();
                    break;
                    case TYPE_2:
                    holder2 = (viewHolder2) convertView.getTag();
                    break;
                }
                    //设置资源
                switch(type)
                {
                    case TYPE_1:
                    holder1.textView.setText(Integer.toString(position));
                    holder1.checkBox.setChecked(true);
                    break;
                    case TYPE_2:
                    holder2.textView.setText(Integer.toString(position));
                    holder2.imageView.setBackgroundResource(R.drawable.icon);
                    break;
                }
            }
            return convertView;
        }
    }
    //各个布局的控件资源
    class viewHolder1{
        CheckBox checkBox;
        TextView textView;
    }
    class viewHolder2{
        ImageView imageView;
        TextView textView;
    }
    
    
    

    当然getItemViewType也可以根据对象类型来设置。像微信聊天界面就是用下面这种方法来判断布局:


    getItemViewType

    总结完毕!以上。


    相关文章

      网友评论

          本文标题:ListView的优化

          本文链接:https://www.haomeiwen.com/subject/fooilttx.html