• ListView优化-通用ViewHolder编写备份


    ViewHolder.java

    package cn.edu.bzu.util;
    
    import android.content.Context;
    import android.util.SparseArray;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * Created by monster on 2015/4/20.
     *  Have a good package ViewHolder
     *  get(Context context,View convertView,ViewGroup parent,int layoutId,int position) To make ViewHolder
     *
     */
    public class ViewHolder {
        private SparseArray<View> mViews; //稀疏数组,SparseArray比HashMap执行效率高
        private int mPosition;
        private View mConvertView; //作用是方便系统在重写UI时,能重用原来实用过的View实例,以此来降低系统资源的消耗和提高代码效率
        /*
         *ConvertView的几点特征。
         *对于一个ListView,Android保存所有曾经生成过的ConvertView实例,直至系统垃圾回收这些实例位置,而不是只保存最后使用的ConvertView对象。
         *这些保存的ConvertView以使用时间顺序排序,并依次被传递到getView函数中。
         */
        public ViewHolder(Context context,ViewGroup parent,int layoutId,int position){
            //ViewHolder的构造方法
            this.mPosition=position;
            this.mViews=new SparseArray<View>(); //初始化SparseArray对象
            mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
            mConvertView.setTag(this);
        }
    
        public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){
            if(convertView==null){
                   return new ViewHolder(context,parent,layoutId,position);
            }else{
                ViewHolder holder=(ViewHolder)convertView.getTag();
                holder.mPosition=position;
                return holder;
            }
        }
    
        /**
         * 通过viewId获取控件
         * @param viewId
         * @param <T>
         * @return
         */
        public <T extends View> T getView(int viewId){
            View view=mViews.get(viewId);
            if(view==null){
                view=mConvertView.findViewById(viewId);
                mViews.put(viewId,view);
            }
            return (T)view;
        }
        public View getConvertView() {
            return mConvertView;
        }
    }
    

    MyAdapterWithCommonViewHolder.java

    package cn.edu.bzu.adapter;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    
    import cn.edu.bzu.entity.Bean;
    import cn.edu.bzu.listview_demo.R;
    import cn.edu.bzu.util.ViewHolder;
    
    /**
     * Created by monster on 2015/4/19.
     */
    public class MyAdapterWithCommonViewHolder extends BaseAdapter {
        private LayoutInflater mInflater; //LayoutInflater是用来找layout下xml布局文件,并且实例化
        private List<Bean> mlist;
        private Context mcontext;
        public MyAdapterWithCommonViewHolder(Context context, List<Bean> list) {
            mcontext=context;
            mInflater=LayoutInflater.from(context); //得到初始化上下文
            mlist=list;
        }
        @Override
        public int getCount() {
            return mlist.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mlist.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=ViewHolder.get(mcontext,convertView,parent,R.layout.listview_item,position);
            ((TextView)holder.getView(R.id.id_title)).setText(mlist.get(position).getTitle());
            ((TextView)holder.getView(R.id.id_desc)).setText(mlist.get(position).getDesc());
            ((TextView)holder.getView(R.id.id_time)).setText(mlist.get(position).getDate());
            ((TextView)holder.getView(R.id.id_phone)).setText(mlist.get(position).getPhone());
            return holder.getConvertView();
        }
    
    }
    

    【源代码下载链接:https://github.com/monsterLin/ViewHolder_Package

  • 相关阅读:
    Java单例模式(Singleton)以及实现
    golang 垃圾回收机制
    MySQL索引背后的数据结构及算法原理
    简述拥塞控制的四种基本算法
    分库分表
    lvalue & rvalue
    理解linux cpu load
    android使用百度地图SDK获取定位信息
    iOSUIWebView---快停下啦,你的愚蠢的行为
    【翻译自mos文章】当/var/tmp文件夹被remove掉之后,GI crash,并启动失败,原因是ohasd can not create named pipe
  • 原文地址:https://www.cnblogs.com/boy1025/p/4473461.html
Copyright © 2020-2023  润新知