ListView简介:
ListView是Android中最常用的控件之一,比如刷微博,联系人列表,等。以垂直的列表形式显示列表项。下面是<<第一行代码>>书中的例子。摘取出来以供分享.
ListView的简单用法:
修改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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
></ListView>
</RelativeLayout>
在布局中加入了ListView控件,宽高都是match_parent。
接下来修改MainActivity:
public class MainActivity extends Activity {
private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","LAIZZ","FSDFF","FSDF"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listview=(ListView)findViewById(R.id.list_view);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
数据中的数据无法直接传递给ListView,需借助适配器来完成。其中一个就是ArrayAdapter,它可以通过泛型来指定适配的数据类型,然后在构造函数中传入数据。最后调用setAdapter()方法将适配器对象传进去.运行程序如图:
定制ListView的界面:
为了显示更加丰富的内容,我们现在对其进行定制.
首先准备一组图片,分别对应每一种水果。然后对其进行描述.
新建类Fruit,:
public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.imageId=imageId;
this.name=name;
}
public String getname(){
return this.name;
}
public int getimage(){
return this.imageId;
}
}
然后为ListView的子项指定一个布局。新建fruit_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="match_parent"
>
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
定义一个ImageView显示水果图片,又定义一个TextView显示水果名称.
接下来创建一个自定义适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类。新建类FruitAdapter:
public class FruitAdapter extends ArrayAdapter<Fruit> {
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
// TODO 自动生成的构造函数存根
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO 自动生成的方法存根
Fruit fruit=getItem(position);
View view=LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, null);
ImageView fruitimage=(ImageView)view.findViewById(R.id.fruit_image);
TextView fruitname=(TextView)view.findViewById(R.id.fruit_name);
fruitimage.setImageResource(fruit.getimage());
fruitname.setText(fruit.getname());
return view;
}
}
FruitAdapter重写父类的一组构造函数,用于将上下文,ListView子项布局的ID和数据传递进来.另外重写了getView()方法,这个方法在每个子项滚动到屏幕的时候调用。
下面修改MainActivity中的代码:
public class MainActivity extends Activity {
private List<Fruit>fruitList=new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter=new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listview=(ListView)findViewById(R.id.list_view);
listview.setAdapter(adapter);
}
private void initFruits() {
// TODO 自动生成的方法存根
Fruit apple=new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana=new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange=new Fruit("Orange", R.drawable.apple_pic);
fruitList.add(orange);
Fruit watermelon=new Fruit("Watermelon", R.drawable.banana_pic);
fruitList.add(watermelon);
Fruit pear=new Fruit("Pear", R.drawable.apple_pic);
fruitList.add(pear);
Fruit Grape=new Fruit("Grape", R.drawable.banana_pic);
fruitList.add(Grape);
Fruit Cheery=new Fruit("Cheery", R.drawable.apple_pic);
fruitList.add(Cheery);
Fruit Mango=new Fruit("Mango", R.drawable.banana_pic);
fruitList.add(Mango);
}
运行效果如图: