码迷,mamicode.com
首页 > 其他好文 > 详细

ListView性能优化

时间:2016-05-12 13:46:25      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1.在MainActivity中,定义MyAdapter继承BaseAdapter,并且实现getCount(),getItem(),getItemId(),getView()的方法。在getView()方法中进行数据填充并且返回列表项视图。使用ListView的setAdapter()方法设置MyAdapter适配器。
2.性能优化:
(1)在getView(int position, View convertView, ViewGroup parent)方法中,重复使用convertView减少对象的创建。
(2)在MyAdapter中定义内部类ViewHolder,用于保存第一次查找的组件,避免下次重复查找。

MainActivity如下:

public class MainActivity extends ActionBarActivity {

    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter(this));
    }
    static class MyAdapter extends BaseAdapter{
        private String[] titles = {"title-1","title-2","title-3","title-4","title-5",
                "title-6","title-7","title-8","title-9","title-10",
                "title-11","title-12","title-13"};
        private int[] icons = {android.R.drawable.ic_input_add,
                android.R.drawable.ic_delete,
                android.R.drawable.ic_dialog_email,
                android.R.drawable.ic_dialog_info,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map,
                android.R.drawable.ic_dialog_map};
        private Context context;
        public MyAdapter(Context context){
            this.context = context;
        }
        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
         //优化第二步:使用ViewHolder提高在容器中查找组件的效率
            ViewHolder vh;
            //优化1:重复使用convertView,减少对象的创建
            if(convertView==null){
                LayoutInflater inflater = LayoutInflater.from(context);
                //实例化一个布局文件
                convertView = inflater.inflate(R.layout.list_item,null);

                vh = new ViewHolder();
                vh.iv = (ImageView) convertView.findViewById(R.id.imageView);
                vh.tv = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(vh);

            }else{
                vh = (ViewHolder) convertView.getTag();
            }
             vh.tv.setText(titles[position]);
            vh.iv.setImageResource(icons[position]);

            return convertView;
        }
        //内部类,用于保存第一次查找的组件,避免下次重复查找
        static class ViewHolder{
            ImageView iv;
            TextView tv;
        }
    }

activity_main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

list_item.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="16dp">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:paddingLeft="10dp"/>
</LinearLayout>

ListView性能优化

标签:

原文地址:http://blog.csdn.net/sz136325/article/details/51364479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!