首页 > 其他 > 详细

ListView优化总结

时间:2015-04-25 18:28:25      阅读:126      评论:0      收藏:0      [点我收藏+]

技术分享

1.适配器优化

1.1ViewHolder类封装

1.ViewHolder类
原始类
 class ViewHolder{
     TextView name;
     TextView age;
 }
封装类
/**
 * 封装视图类
 * @author nate
 */
public class CommViewHolder
{
    /**
     * 视图容器
     */
    private SparseArray<View> mViews;

    /**
     * 位置标识
     */
    private int mPosition;

    /**
     * 视图
     */
    private View mConvertView;

    /**
     * 私有化构造方法,不让外界进行使用
     * @param context
     * @param parent
     * @param layoutId
     * @param position
     */
    private CommViewHolder(Context context, ViewGroup parent, int layoutId, int position)
    {
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        this.mPosition = position;
        mConvertView.setTag(this);
    }

    /**
     * 
     * @param context
     * @param parent
     * @param layoutId
     * @param position
     * @param convertView
     * @return 静态方法获取到viewholder类实例
     */
    public static CommViewHolder get(Context context, ViewGroup parent, int layoutId, int position, View convertView)
    {
        if(convertView == null)
        {
            return new CommViewHolder(context, parent, layoutId, position);
        }
        else
        {
            CommViewHolder holder = (CommViewHolder) convertView.getTag();
            // 复用视图,但是position要更新
            holder.mPosition = position;
            return holder;
        }
    }

    /**
     * 
     * @param viewId 控件id
     * @return 根据控件id获取到控件
     */
    @SuppressWarnings("unchecked")
    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;
    }

    /**
     * @return 返回视图类
     */
    public View getmConvertView()
    {
        return mConvertView;
    }

    /**
     * @param viewId
     * @param content
     * @return 设置textview相关
     */
    public CommViewHolder setTextView(int viewId, String content)
    {
        TextView tv = getView(viewId);
        tv.setText(content);
        return this;
    }

    // TODO 可以根据自己的需要编写更多适用的方法。。。
}
2.Adapter类中Viewholder代码对比
使用原始holder
 class MyAdapter extends ArrayAdapter<Person>
    {

        public MyAdapter(Context context, int resource, List<Person> objects)
        {
            super(context, resource, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder = null;
            if(convertView == null)
            {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, null);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.age = (TextView) convertView.findViewById(R.id.age);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            Person person = getItem(position);
            holder.name.setText(person.getName());
            holder.age.setText(person.getAge());

            return convertView;
        }

        class ViewHolder
        {
            TextView name;

            TextView age;
        }
    }
使用封装holder
class MyAdapter extends ArrayAdapter<Person>{
        public MyAdapter(Context context, int resource, List<Person> objects)
        {
            super(context, resource, objects);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            Person person = getItem(position);
            CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, R.layout.item_layout, position,convertView);
            viewHolder.setTextView(R.id.name, person.getName()).setTextView(R.id.age, person.getAge());
            return viewHolder.getmConvertView();
        }
    }

1.2.Adapter类封装

1.原始类

class MyAdapter extends ArrayAdapter<Person>
    {

        public MyAdapter(Context context, int resource, List<Person> objects)
        {
            super(context, resource, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            Person person = getItem(position);
            CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, R.layout.item_layout, position,
                    convertView);
            viewHolder.setTextView(R.id.name, person.getName()).setTextView(R.id.age, person.getAge());
            return viewHolder.getmConvertView();
        }
    }

2.封装类

public abstract class CommAdapter<T> extends ArrayAdapter<T>
{
    private int resourceId;
    public CommAdapter(Context context, int resource, List<T> objects)
    {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, resourceId, position, convertView);
        setConvert(viewHolder, getItem(position));
        return viewHolder.getmConvertView();
    }
    public abstract void setConvert(CommViewHolder viewHolder, T t);
}

2.ListViewItem不可点击

2.1.原因

抢占焦点控件抢占了焦点:列如button  checkbox等

2.2.解决方法

方法一:为抢占焦点的控件布局添加:android:focusable="false";
方法二:为整个listview的item布局添加android:descendantFocusability="blocksDescendants"

3.ListView复用导致内容错乱

3.1.checkBox错误选中效果解决

1.未bean对象添加isChecked属性,更具isChecked属性在adapter中动态设置checkBox是否选中
2.在adapter中设置一个记录选中checkbox的list集合;
final CheckBox cb = viewHolder.getView(R.id.cb);
                cb.setChecked(false);
                if(mPos.contains(viewHolder.getPosition()))
                {
                    cb.setChecked(true);
                }

                cb.setOnClickListener(new OnClickListener()
                {

                    @Override
                    public void onClick(View v)
                    {
                        Log.d("guxuewu", "点击了cb");
                        if(cb.isChecked())
                        {
                            mPos.add(viewHolder.getPosition());
                        }
                        else
                        {
                            //希望执行remove object方法,所以进行强转
                            mPos.remove((Integer) viewHolder.getPosition());
                        }
                    }
                });

ListView优化总结

原文:http://blog.csdn.net/u011771755/article/details/45270705

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!