ListView基本示例

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。

第三個參數:自定義的數據名稱。






源碼下載






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章