Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

ListView 進階系列之一:讓列表內容依序淡入顯示

編輯:進階開發

ListView 可以說是 Android 開發中使用頻率最高的控制項之一,而為它添加絢麗的特效,幾乎是日後開發中在所難免的課題。這個系列的第一篇,就來教你如何讓列表內容以淡入的方式依序優雅呈現。

先實作一個基礎的 ListView

這部分想必大家都不陌生,主要分為三個步驟:其一,在版面檔(main.xml)中添加 ListView 控制項;其二,在 layout 中新建一個 ListView 每一項要顯示的內容版面(lvitem.xml);其三,在程式碼中取得 ListView 的引用,為它設定 Adapter 並加入資料。以下直接看程式碼。

main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@+id/listv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layoutAnimation="@anim/animationlayout"
        />
    <Button
        android:id="@+id/buttonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="測試"
        />
</LinearLayout>

lvitem.xml

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/lvitem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns=" * " >
    <TableRow>
        <TextView android:id="@+id/tvname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
        <TextView android:id="@+id/tvage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
        <TextView android:id="@+id/tvsex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
    </TableRow>
</TableLayout>

DemoActivity.java

package cn.edu.heut.zcl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class DemoActivity extends Activity {
    /** Called when the activity is first created. */
    ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listv);
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", "n" + i);
            map.put("age", "age" + i);
            map.put("sex", "s" + i);
            data.add(map);
        }
        String[] from = {"name", "age", "sex"};
        int[] to = {R.id.tvname, R.id.tvage, R.id.tvsex};
        SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.lvitem, from, to);
        lv.setAdapter(sa);
    }
}

添加淡入特效

接下來才是本文的重點。ListView 的特效是透過 Animation 來實現的。首先,在 res 中新增一個名為 anim 的資料夾,並在其中建立一個 animationSet 的 XML 檔(animatonset1.xml),加入我們要使用的特效:

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="1000" />
</set>

接著要使用 LayoutAnimationController,這裡簡要介紹它的作用:它用於為一個 Layout 裡面的控制項,或是一個 ViewGroup 裡面的控制項設定動畫效果;每一個控制項都會擁有相同的動畫效果;而且可以設定每個控制項動畫的時間差,這些工作既能在 XML 中完成,也能在程式碼中實現。具體的 animationlayout.xml 如下:

<layoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="random"
    android:animation="@anim/animatonset1" />

其中 android:delay 控制每個項目出現的時間間隔,android:animationOrder 則決定了動畫的播放順序(此處設為 random 隨機)。最後,只要在前面寫好的 ListView 的 XML 宣告處,加上 android:layoutAnimation="@anim/animationlayout" 這一行,就能為列表掛上這段動畫效果了。

如此一來,當列表載入時,每一項目便會以淡入的方式依序優雅浮現,為使用者帶來更細膩、更順暢的視覺體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved