Android UI 之 ListView

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   color   java   使用   

一、在代碼中建立(不適用XML布局檔案)

1.建立一個項目:ListViewLearn

2.修改MainActivity,繼承於ListActivity

3.建立一個String數組,用來儲存ListView中的現實內容

package com.learn.listviewlearn.utility;public class Util {    public static final String[] COUNTRYS = { "中國", "美國", "俄羅斯", "英國", "法國" };}

4.修改onCreate方法,設定一個Adapter,數組中的內容在ListView中現實出來

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // setContentView(R.layout.activity_main);        this.setListAdapter(new ArrayAdapter<String>(this,                android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS));    }

二、使用XML布局檔案來定義ListView的樣式

1.修改activity_main.xml檔案,添加一個ListView,id必須為android:id="@android:id/list"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@android:id/list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#FFFFFF00" >    </ListView></LinearLayout>

2.修改OnCreate方法

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        this.setListAdapter(new ArrayAdapter<String>(this,                android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS));    }

三、使用XML布局檔案來定義ListViewItem的樣式

 1.首先建立一個list_view.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" >    <ImageView        android:id="@+id/imageViewIcon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:contentDescription="@string/icon" >    </ImageView>    <TextView        android:id="@+id/textViewContent"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >    </TextView></LinearLayout>

2.然後建立一個Adapter繼承於BaseAdapter,主要修改getCount()和getView()方法

package com.learn.listviewlearn.adapter;import com.learn.listviewlearn.R;import com.learn.listviewlearn.utility.Util;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {    private Context context;    public ListViewAdapter() {        // TODO Auto-generated constructor stub    }    @Override    public int getCount() {        return Util.COUNTRYS.length;    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return null;    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return 0;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);            ItemViewCache itemViewCache = new ItemViewCache();            itemViewCache.imageView = (ImageView)convertView.findViewById(R.id.imageViewIcon);            itemViewCache.textView = (TextView)convertView.findViewById(R.id.textViewContent);            convertView.setTag(itemViewCache);        }                ItemViewCache cache = (ItemViewCache) convertView.getTag();        cache.imageView.setImageResource(Util.images[position]);        cache.textView.setText(Util.COUNTRYS[position]);        return convertView;    }        private static class ItemViewCache{        public TextView textView;        public ImageView imageView;    }}

Util.java

package com.learn.listviewlearn.utility;import com.learn.listviewlearn.R;public class Util {    public static final String[] COUNTRYS = { "中國", "美國", "俄羅斯", "英國", "法國" };    public static final int[] images = { R.drawable.ic_launcher,            R.drawable.ic_launcher, R.drawable.ic_launcher,            R.drawable.ic_launcher, R.drawable.ic_launcher };}

四、為ListView添加Click事件。只需要在MainActivity中實現onListItemClick()方法

    @Override    protected void onListItemClick(ListView l, View v, int position, long id) {        Toast.makeText(this, "你選擇了" + Util.COUNTRYS[position], Toast.LENGTH_SHORT)                .show();    }

源碼:HelloListView.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.