大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > Android UI 之 ListView

Android UI 之 ListView(2)

关键词:AndroidUIListView  阅读(1081) 赞(12)

[摘要]本文是对Android UI 之 ListView的讲解,对学习Android编程技术有所帮助,与大家分享。

三、使用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();
    }

源码:http://files.cnblogs.com/zhangtingkuo/HelloListView.zip

«上一页12下一页»


相关评论