Android學習之——ListView

來源:互聯網
上載者:User

標籤:des   android   cWeb   style   blog   http   io   ar   color   

 背景知識  

      ListView在Android應用中使用非常廣泛,手機上必備的微博、網易新聞等,都使用了ListView。

                            

定義

ListView是一個展示可滾動項的列表的視圖集合(View Group)。通過適配器(Adapter)從內容來源(例如一個數組或資料庫查詢)取得內容

然後將每個項轉化為呈現在列表中的View,清單項目自動地被插入到列表中。

                 

Demo

ListView的實現過程:

            準備資料---->使用動態數組儲存資料----->構建Adapter(適配器)----->添加Adapter到listView上,並顯示

 

 MainActivity代碼

 1 package com.johntsai.listviewdemo; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import android.app.Activity; 9 import android.os.Bundle;10 import android.view.View;11 import android.widget.AdapterView;12 import android.widget.AdapterView.OnItemClickListener;13 import android.widget.ListView;14 import android.widget.SimpleAdapter;15 import android.widget.Toast;16 17 public class MainActivity extends Activity {18     private ListView listView;19     private SimpleAdapter adapter;20     //準備資料21     private String[] listTitle = {22             "2015年春運火車票今起發售",23             "馬爾代夫遭淡水危機 中國送水",24             "中國5架戰機飛躍日本宮古海峽"25     };26     private String[] listContent = {27             "可用網路、電話購票,3個時段可嘗試\"撿漏\"",28             "淡水生產裝置受損,淡水供應告急,15萬居民面臨水荒",29             "於6日飛躍宮古海峽,赴西太平洋訓練"30     };31     private List<Map<String,Object>> listItems;32     //儲存資料33     private void getData(){34         listItems = new35                 ArrayList<Map<String,Object>>();36         for(int i = 0 ; i < listTitle.length; i++){37             Map<String,Object> listItem =38                     new HashMap<String, Object>();39             listItem.put("image",R.drawable.ic_launcher);40             listItem.put("title", listTitle[i]);41             listItem.put("content", listContent[i]);42             listItems.add(listItem);43         }44     }45     @Override46     protected void onCreate(Bundle savedInstanceState) {47         super.onCreate(savedInstanceState);48         setContentView(R.layout.activity_main);49         50         getData();51         listView = (ListView)findViewById(R.id.listView);52         //構建Adapter53         //第一個參數:context,即與適配器的View啟動並執行Context54         //第二個參數:data55         //第三個參數:resource,為listView項定義視圖的布局id56         //第四個參數:from57         //第五個參數:to,要顯示到的View的id58         adapter = new SimpleAdapter(this,59                 listItems ,60                 R.layout.listview_item, 61                 new String[] {"image","title","content"},62                 new int[] {R.id.list_image,R.id.item_title,R.id.item_content});63         //添加adapter到listView並顯示64         listView.setAdapter(adapter);65         66         //為listView項添加響應監聽事件67         listView.setOnItemClickListener(new OnItemClickListener() {68 69             @Override70             public void onItemClick(AdapterView<?> parent, View view,71                     int position, long id) {72                 Toast.makeText(MainActivity.this,73                             "你點擊的是標題為"+listTitle[position], 74                             Toast.LENGTH_LONG).show();75             }76         });77         78     }79 }

 

MainActivity介面布局檔案

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical"> 6      7     <ListView 8         android:id="@+id/listView" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         >12     </ListView>13 </LinearLayout>

 

 

 

ListView項布局檔案

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:id="@+id/ListViewItem" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:orientation="horizontal" > 7      8     <ImageView  9         android:id="@+id/list_image"10         android:layout_width="50dp"11         android:layout_height="50dp"12         android:src="@drawable/ic_launcher"13         />14      <LinearLayout 15         android:id="@+id/list_content" 16         android:layout_width="fill_parent"17         android:layout_height="50dp"18         android:orientation="vertical"19          >20         <TextView 21             android:id="@+id/item_title"22             android:layout_width="match_parent"23             android:layout_height="0dp"24             android:layout_weight="1"25             />26          <TextView 27             android:id="@+id/item_content"28             android:layout_width="match_parent"29             android:layout_height="0dp"30             android:layout_weight="1"31             android:textSize="10sp"32             android:textColor="@android:color/darker_gray"33              />34      </LinearLayout>35 </LinearLayout>

  Demo運行效果:

        

 

Android學習之——ListView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.