这次来说说listview的优化使用方法,我们其实都对listview已经很熟悉了,不过我在上两篇写的是最基础的用法,现在我们来深入一下,加深对listview使用的了解和相关的优化。
listview最经常加载的适配器例如ArrayAdapter和SimpleAdapter是如何来的,其实都是源自于一个基础类,名为BaseAdapter。
如荧光笔标记出来的,那么我们就来自己写个类去继承android.widget.BaseAdapter,不也是可以创造出属于自己的适配器吗?说干就干!
首先当然还是在主界面添加上我们的listview。
<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"
tools:context="com.exam.listviewdemotest.MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
然后就是我们将在适配器中加入的布局内容,在这里我们再建一个item.xml来实现内容布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Title"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:layout_toRightOf="@+id/iv_image"
android:text="content"
android:textSize="20sp" />
</RelativeLayout>
and,我们此时来新建一个类,负责对所有加载在内容布局的对象的封装:
package com.exam.listviewdemotest;
public class MyItemObject {
public int imageId;
public String title;
public String content;
public MyItemObject(int imageId,String title,String content) {
this.imageId = imageId;
this.title = title;
this.content = content;
}
}
没啥好说,好了接下来我们来主界面的类中来对各个对象进行相应的赋值:
public class MainActivity extends ActionBarActivity {
private int[] res = new int[] { R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6,
R.drawable.img7, R.drawable.img8, R.drawable.img9,
R.drawable.img10, R.drawable.img11, R.drawable.img12,
R.drawable.img13, R.drawable.img14, R.drawable.img15 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<MyItemObject> itemObjects = new ArrayList<MyItemObject>();
for (int i = 0; i < 15; i++) {
itemObjects.add(new MyItemObject(res[i], "这是第" + i + "个题目", "这是第"
+ i + "条内容"));
}
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter(this, itemObjects));
}
}
恩,很easy对不对?我只是在这里对一些图片放在数组里面,使每一项的图片都有所不同。好了,下一步就是重头戏了,我们写个类去继承BaseAdapter,我在这里写了个MyAdapter类去继承。
public class MyAdapter extends BaseAdapter {
private List<MyItemObject> mList;
private LayoutInflater inflater;
public MyAdapter(Context context,List<MyItemObject> list) {
this.mList = list;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView textView_title = (TextView) view.findViewById(R.id.tv_title);
TextView textView_content = (TextView) view.findViewById(R.id.tv_content);
MyItemObject itemObject = mList.get(position);
imageView.setImageResource(itemObject.imageId);
textView_title.setText(itemObject.title);
textView_content.setText(itemObject.content);
return view;
}
}
这段代码有点长,我来解释一下,这里继承了BaseAdapter后就会重写四个方法,第一个方法getCount(),返回的是一个整型数值,也就是数值的长度或者说数量大小,我们直接返回大小即可,第二个方法是getItem(),也就是我们所显示的页面,我们返回此时的页面即可,然后第三个方法没得好说,最后一个方法是重心,我们创建出一个view对象,再用view去加载我们的布局文件,然后一个个组件去声明,最后加载布局内容,然后将view返回就可以了。
以上就是普通的listview的适配器的写法,其实我们还可以去进一步优化,详情看下一篇。没图没XX,所以文章最后附上运行效果图: