Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實現 ListView 列表並點擊跳轉到對應頁面

編輯:關於 Android 程式設計

做這個小範例純粹是為了練習一點小知識,因為自己才剛入門。實作方式其實很簡單:定義一個 ListView,然後設定監聽即可。ListView 對每個條目的監聽是 setOnItemClickListener,其回調方法 onItemClick 的四個參數分別是:

parent:觸發點擊動作的 AdapterView。

view:在 AdapterView 中被點擊的視圖(由 adapter 提供)。

position:該視圖在 adapter 中的位置。

id:被點擊元素的行 id。

下面看看 XML 裡的程式碼。

1. activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/Lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

2. item.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/p1">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="30sp"
        android:text="@string/name">
    </TextView>
</LinearLayout>

3. model.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/Iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/p1">
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈啊哈哈">
    </TextView>
</LinearLayout>

接著是 MainActivity.java,它的功能是顯示 ListView;用戶點擊其中一個 item 後,我們就能取得對應資訊,再透過 Bundle 把資料送到跳轉頁面進行展示:

package com.example.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private ListView Lv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Lv = (ListView) findViewById(R.id.Lv);
        final String[] name = new String[] { "張國榮", "張學友", "譚詠麟" };
        final String[] message = new String[] {
            "張國榮,1956年生於香港,歌手、演員、音樂人;影視歌多棲發展的代表之一。1977年正式出道。1983年以《風繼續吹》成名。",
            "張學友,歌手、演員,1961年出生於香港,1984年獲得香港首屆十八區業余歌唱大賽冠軍正式出道,1993年發行的國語唱片《吻別》年度銷量超過400萬張。",
            "譚詠麟,1950年出生於香港,籍貫廣東新會,中國香港男歌手、音樂人、演員。1973年任溫拿樂隊主音歌手,1978年溫拿樂隊解散後以個人身份發展。"
        };
        final int[] photo = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3 };
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("photo", R.drawable.p1);
        map1.put("name", name[0]);
        data.add(map1);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("photo", R.drawable.p2);
        map2.put("name", name[1]);
        data.add(map2);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("photo", R.drawable.p3);
        map3.put("name", name[2]);
        data.add(map3);
        Lv.setAdapter(new SimpleAdapter(this, data, R.layout.item,
                new String[] { "photo", "name" },
                new int[] { R.id.iv, R.id.tv_name }));
        Lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Bundle bundle = new Bundle();
                bundle.putInt("photo", photo[arg2]);
                bundle.putString("message", message[arg2]);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, MoveList.class);
                Log.i("message", message[arg2]);
                startActivity(intent);
            }
        });
    }
}

這是使用者點擊後跳轉到的頁面 MoveList:

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MoveList extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.model);
        Bundle bundle = getIntent().getExtras();
        int id = bundle.getInt("photo");
        String message = bundle.getString("message");
        ImageView Iv = (ImageView) findViewById(R.id.Iv);
        Iv.setImageResource(id);
        TextView tv = (TextView) findViewById(R.id.tv_message);
        tv.setText(message);
    }
}

最後提醒大家,千萬別忘了在 AndroidManifest.xml 裡註冊新頁面,這一點要特別小心。

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