Android ListView 常用用法(一)

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

ListView是比較常用的控制項,但一直都覺得建立ListView步驟有點繁瑣,故在此總結一下,方便查閱。

程式效果是實現一個ListView,ListView裡面有標題,內容和圖片,並加入點擊和長按響應。

 

 

 

首先在xml裡面定義一個ListView

<?xml version="1.0" encoding="utf-8"?><LinearLayout     android:id="@+id/LinearLayout01"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     xmlns:android="http://schemas.android.com/apk/res/android"><ListView android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:id="@+id/ListView01"          /></LinearLayout>
main.xml

定義ListView每個條目的Layout,用RelativeLayout實現:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout     android:id="@+id/RelativeLayout01"     android:layout_width="fill_parent"     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_height="wrap_content"     android:paddingBottom="4dip"     android:paddingLeft="12dip"    android:paddingRight="12dip"><ImageView     android:paddingTop="12dip"    android:layout_alignParentRight="true"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/ItemImage"    /> <TextView     android:text="TextView01"     android:layout_height="wrap_content"     android:textSize="20dip"     android:layout_width="fill_parent"     android:id="@+id/ItemTitle"    /><TextView     android:text="TextView02"     android:layout_height="wrap_content"     android:layout_width="fill_parent"     android:layout_below="@+id/ItemTitle"     android:id="@+id/ItemText"    /></RelativeLayout>
list_items.xml

最後在Activity裡面調用和加入Listener,具體見注釋:

package com.ray.test;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.MenuItem;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.view.View.OnCreateContextMenuListener;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;public class TestListView extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //綁定Layout裡面的ListView        ListView list = (ListView) findViewById(R.id.ListView01);                //產生動態數組,加入資料        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();        for(int i=0;i<10;i++)        {            HashMap<String, Object> map = new HashMap<String, Object>();            map.put("ItemImage", R.drawable.checked);//映像資源的ID            map.put("ItemTitle", "Level "+i);            map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");            listItem.add(map);        }        //產生適配器的Item和動態數組對應的元素        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//資料來源             R.layout.list_items,//ListItem的XML實現            //動態數組與ImageItem對應的子項                    new String[] {"ItemImage","ItemTitle", "ItemText"},             //ImageItem的XML檔案裡面的一個ImageView,兩個TextView ID            new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}        );               //添加並且顯示        list.setAdapter(listItemAdapter);                //添加點擊        list.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,                    long arg3) {                setTitle("點擊第"+arg2+"個項目");            }        });              //添加長按點擊        list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {                        @Override            public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {                menu.setHeaderTitle("長按菜單-ContextMenu");                   menu.add(0, 0, 0, "彈出長按菜單0");                menu.add(0, 1, 0, "彈出長按菜單1");               }        });     }        //長按菜單響應函數    @Override    public boolean onContextItemSelected(MenuItem item) {        setTitle("點擊了長按菜單裡面的第"+item.getItemId()+"個項目");         return super.onContextItemSelected(item);    }}
TestListView。java

 

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.