1. 程式人生 > >Android零基礎入門第39節:ListActivity和自定義列表項

Android零基礎入門第39節:ListActivity和自定義列表項

arraylist component save 高速 ram 如果 view設置 ren 屬性

相信通過前兩期的學習,以及會開發最簡單的一些列表界面了吧,那麽本期接著來學習更多方法技巧。

技術分享

一、使用ListActivity

如果程序的窗口僅僅需要顯示一個列表,則可以直接讓Activity繼承ListActivity來實現, ListActivity的子類無須調用setContentView()方法來顯示某個界面,而是可以直接傳入一個內容Adapter,ListActivity的子類就呈現出一個列表。

接下來通過一個簡單的示例程序來學習基於ListActivity實現列表。

繼續使用WidgetSample工程的listviewsample模塊,在java包下創建MyListActivity.java文件,具體代碼如下:

package com.jinyu.cqkxzsxy.android.listviewsample;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MyListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        
// 定義一個數組 String[] persons = {"蠟筆小新", "皮卡丘", "蜘蛛俠", "灰太狼", "黑貓警長", "孫悟空", "忍者神龜", "米老鼠", "HelloKitty", "櫻桃小丸子"}; // 將數組包裝成ArrayAdapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, persons);
// 設置該窗口顯示列表 setListAdapter(adapter); } }

ListActivity的布局文件中只有一個ListView,只需要為ListActivity設置Adapter即可。

運行程序,可以看到下圖所示界面效果。

技術分享

從上圖可以看到,ListActivity的默認布局是由一個位於屏幕中心的列表組成的。

二、自定義列表項

前面學習ListView都是使用的Android系統自定義列表項資源,基本都是一些純文本的資源,界面不夠炫目,也沒有辦法定制。在實際開發中,列表經常包括圖標、按鈕等組件,這就需要開發者自定義列表項來完成了。關鍵是需要給適配器Adapter提供足夠的數據,讓Adapter能夠用更豐富的View對象來填充列表的每一行。

接下來就通過一個示例來學習如何自定義列表項。

同樣使用WidgetSample工程的listviewsample模塊,在app/main/res/layout/目錄下創建custom_item_layout.xml文件,在其中填充如下代碼片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

在res/layout/目錄下新建一個custom_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"
              android:orientation="horizontal"
              android:gravity="center_vertical">

    <ImageView
        android:id="@+id/icon_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/content_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#00f"/>
</LinearLayout>

這個布局使用LinearLayout作為一行,其中圖標位於左側,文本位於右側。

接下來為ListView提供Adapter,Adapter決定了ListView所要顯示的列表項。在java包下創建CustomItemActivity.java文件,加載上面新建的布局文件,具體代碼如下:

package com.jinyu.cqkxzsxy.android.listviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CustomItemActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_item_layout);

        // 獲取界面ListView組件
        ListView listView = (ListView) findViewById(R.id.listview);

        // 定義一個List集合
        final List<String> components = new ArrayList<>();
        components.add("TextView");
        components.add("EditText");
        components.add("Button");
        components.add("CheckBox");
        components.add("RadioButton");
        components.add("ToggleButton");
        components.add("ImageView");

        // 將List包裝成ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.custom_item, R.id.content_tv, components);

        // 為ListView設置Adapter
        listView.setAdapter(adapter);

        // 為ListView列表項綁定點擊事件監聽器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Toast.makeText(CustomItemActivity.this, components.get(position),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

以上代碼遵循前面學習的ListView示例的整體結構。其主要的區別就是使用了自定義列表布局R.layout.list_item。創建ArrayAdapter必須指定如下四個參數。

  • context:要使用的上下文環境,幾乎創建所有組件都需要傳入Context對象。

  • resource: 要使用的自定義列表項布局資源 ID。

  • textViewResourceId:自定義列表布局中TextView的ID,該TextView組件將作為ArrayAdapter的列表項組件。

  • objects:要實際顯示的數組或List,將負責為多個列表項提供數據。 該數組或List包含多少個元素,就將生成多少個列表項。

運行程序,可以看到下圖所示界面效果。

技術分享

從上圖可以看到,列表布局裏面使用了我們自定義的圖標,也修改了文本顯示樣式。

但是在這個示例中,所有的圖標都是相同的,往往不能滿足實際開發需求,會在下一節中來進行學習。

今天就先到這裏,如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論微信群,共同成長!

此文章版權為微信公眾號分享達人秀(ShareExpert)——鑫鱻所有,若需轉載請聯系作者授權,特此聲明!

往期總結分享:

Android零基礎入門第1節:Android的前世今生

Android零基礎入門第2節:Android 系統架構和應用組件那些事

Android零基礎入門第3節:帶你一起來聊一聊Android開發環境

Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招

Android零基礎入門第5節:善用ADT Bundle, 輕松邂逅女神

Android零基礎入門第6節:配置優化SDK Manager, 正式約會女神

Android零基礎入門第7節:搞定Android模擬器,開啟甜蜜之旅

Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點

Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發

Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio

Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程

Android零基礎入門第12節:熟悉Android Studio界面,開始裝逼賣萌

Android零基礎入門第13節:Android Studio配置優化,打造開發利器

Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代

Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航

Android零基礎入門第16節:Android用戶界面開發概述

Android零基礎入門第17節:TextView屬性和方法大全

Android零基礎入門第18節:EditText的屬性和使用方法

Android零基礎入門第19節:Button使用詳解

Android零基礎入門第20節:CheckBox和RadioButton使用大全

Android零基礎入門第21節:ToggleButton和Switch使用大全

Android零基礎入門第22節:ImageView的屬性和方法大全

Android零基礎入門第23節:ImageButton和ZoomButton使用大全

Android零基礎入門第24節:自定義View簡單使用,打造屬於你的控件

Android零基礎入門第25節:簡單且最常用的LinearLayout線性布局

Android零基礎入門第26節:兩種對齊方式,layout_gravity和gravity大不同

Android零基礎入門第27節:正確使用padding和margin

Android零基礎入門第28節:輕松掌握RelativeLayout相對布局

Android零基礎入門第29節:善用TableLayout表格布局

Android零基礎入門第30節:兩分鐘掌握FrameLayout幀布局

Android零基礎入門第31節:少用的AbsoluteLayout絕對布局

Android零基礎入門第32節:新推出的GridLayout網格布局

Android零基礎入門第33節:Android事件處理概述

Android零基礎入門第34節:Android中基於監聽的事件處理

Android零基礎入門第35節:Android中基於回調的事件處理

Android零基礎入門第36節:Android系統事件的處理

Android零基礎入門第37節:初識ListView

Android零基礎入門第38節:初識Adapter

技術分享

技術分享

Android零基礎入門第39節:ListActivity和自定義列表項