Android ListView 基础入门 简介以及深入优化

zzy1975 9年前

来自: http://blog.csdn.net//never_cxb/article/details/48159717


ListView 简介

ListView 是安卓里常用的控件, 本文介绍一下常用用法,以及优化等方法

改写 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">        <ListView  android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" />    </LinearLayout>

新建一个 javabean

package com.example.tomchen.testlistview;    /** * Created by tomchen on 9/1/15. */  public class Icon {      private int imageId;      private String name;        public Icon(String name, int imageId) {          this.name = name;          this.imageId = imageId;      }        public String getName() {          return name;      }        public int getImageId() {          return imageId;      }  }

新建icon_item.xml 用于 listview 每个元素的视图

<?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="match_parent" android:orientation="horizontal">        <TextView  android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="match_parent" />        <ImageView  android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="match_parent" />    </LinearLayout>

新建一个class IconAdapter 继承自 ArrayAdapter

package com.example.tomchen.testlistview;    import android.content.Context;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ArrayAdapter;  import android.widget.ImageView;  import android.widget.TextView;    import java.util.List;    /** * Created by tomchen on 9/1/15. */  public class IconAdapter extends ArrayAdapter<Icon> {      private int resourceId;          public IconAdapter(Context context, int resource, List<Icon> icons) {          super(context, resource, icons);          this.resourceId = resource;      }          @Override      public View getView(int position, View convertView, ViewGroup parent) {          Icon icon = getItem(position);          View view;          if (convertView == null) {              view = LayoutInflater.from(getContext()).inflate(resourceId, null);          } else {              view = convertView;          }          ImageView image = (ImageView) view.findViewById(R.id.icon);          image.setImageResource(icon.getImageId());          TextView text = (TextView) view.findViewById(R.id.text);          text.setText(icon.getName());          return view;      }  }

Note
这儿有了一个优化, convertView 将之前的布局进行缓存, 以便之后进行重用.

所以我们判断convertView是否为空. 空的话就使用LayoutInflater去加载布局, 否则的话就直接复用convertView

关于 ArrayAdapter 可以参阅这篇博客
Android ArrayAdapter 源码查看 深入了解

测试listview

MainActivity里面加入下面代码

package com.example.tomchen.testlistview;      import android.support.v7.app.AppCompatActivity;  import android.os.Bundle;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.ListView;    import java.util.ArrayList;  import java.util.List;    public class MainActivity extends AppCompatActivity {      private ListView listView;      private List<Icon> icons = new ArrayList<Icon>();        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          listView = (ListView) findViewById(R.id.listview);          icons.add(new Icon("Icon_01", R.drawable.ic_01));          icons.add(new Icon("Icon_02", R.drawable.ic_02));          icons.add(new Icon("Icon_03", R.drawable.ic_03));          icons.add(new Icon("Icon_04", R.drawable.ic_04));          icons.add(new Icon("Icon_05", R.drawable.ic_05));          icons.add(new Icon("Icon_06", R.drawable.ic_06));          icons.add(new Icon("Icon_07", R.drawable.ic_07));          icons.add(new Icon("Icon_08", R.drawable.ic_08));          icons.add(new Icon("Icon_09", R.drawable.ic_09));          IconAdapter adapter = new IconAdapter(this, R.layout.icon_item, icons);          listView.setAdapter(adapter);      }      }
效果图如下:
图片名称
</div>