ListView在Android中应用十分广泛,如新闻列表、电话号码列表 、联系人列表等等。它一般垂直显示在手同屏幕上,可以自上而下地拉动。一般使用Adapter方法向列表中填充数据,数据来源有很多,如数组、数据库等等。
ListView属性(摘自google官方)
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Attribute Name | Related Method | Description | |||||||||
android:divider | Drawable or color to draw between list items. | ||||||||||
android:divider | setDivider(Drawable) | Drawable or color to draw between list items. | |||||||||
android:dividerHeight | Height of the divider. | ||||||||||
android:entries | Reference to an array resource that will populate the ListView. | ||||||||||
android:footerDividersEnabled | When set to false, the ListView will not draw the divider before each footer view. | ||||||||||
android:headerDividersEnabled | When set to false, the ListView will not draw the divider after each header view. |
示例
(效果图)
strings.xml
<resources>
<string name="app_name">列表基本示例</string>
<string name="action_settings">Settings</string>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#1cb7eb</color>
<color name="colorPrimaryDark">#1cb7eb</color>
<color name="colorAccent">#FF4081</color>
</resources>
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
layout/activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:height="60dp">
</TextView>
mainactivity.java
package com.my.asus.listviewsample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
代码分析
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_listview, mobileArray);
数据源是数组(array)的时候,可以使用adapter。以上表达式的意思是将自定义的数组mobileArray中的项逐个填充到activity_listview.xml中的TextView中。
第一个参数this:一般都用this,指这个应用。
第二个参数:在layout中创建的xml,其中包含用来接受数组项的TextView。
第三个参数:自定义的数据名称。