Android开发————ListView学习笔记

ListView是一个带滚动条垂直显示内容一个视图


下面用例子来学习ListView :


首先创建一个类用来进行主页面显示:

package com.person.listviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListView extends Activity {

	private ListView lv;
	// 创建一个绑定内容的容器,用于将制定的内容绑定到制定的View视图中
	private BaseAdapter adapter = new BaseAdapter() {

		// 自己创建一个ListView中显示的内容,里面的CustomListCellData是另一个
		// 类,其中amap_bus、amap_car、amap_man是三幅下载好的图保存到drawable
		// 文件夹中,用于显示ListView中的图片
		private CustomListCellData[] data = new CustomListCellData[] {
				new CustomListCellData("Bus", "公交", R.drawable.amap_bus),
				new CustomListCellData("Car", "轿车", R.drawable.amap_car),
				new CustomListCellData("Man", "行人", R.drawable.amap_man) };

		@Override
		// getView 只有在屏幕显示出来的时候才可调用该函数出现几个就会调用几次
		// 其中convertView是指已经被回收的View,当视图不可见的时候该视图被操作系统回收到了converView
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout ll = null;
			// 优化显示
			// 这种方式可以充分利用在ListView中显示的内容在回收之后又出现的情况,进行反复创建新的视图
			if (convertView != null) {
				ll = (LinearLayout) convertView;
			} else {
				ll = (LinearLayout) LayoutInflater.from(CustomListView.this)
						.inflate(R.layout.custom_listcell, null);
			}

			// 获取Item内容所在的位置
			CustomListCellData data = getItem(position);

			// 将该ListView中的Item中进行内容填充
			ImageView icon = (ImageView) ll.findViewById(R.id.image1);
			TextView name = (TextView) ll.findViewById(R.id.name);
			TextView dec = (TextView) ll.findViewById(R.id.description);

			// 设置好该布局中的一些内容:图片的ID变量,图片的name变量,图片的description变量
			icon.setImageResource(data.iconId);
			name.setText(data.getName());
			dec.setText(data.getDescription());

			// 返回设置好的显示界面Item
			return ll;
		}

		@Override
		// 从该位置给定的列表所关联的ID
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		// 从特定的位置获取该位置的数据,因为ListView显示的方式中有很多个Item,一个Item中包含的
		// 内容就是一个Class或者其他的所需要填充的东西
		public CustomListCellData getItem(int position) {
			// TODO Auto-generated method stub
			return data[position];
		}

		@Override
		// 列表项的数量
		public int getCount() {
			// TODO Auto-generated method stub
			return data.length;
		}
	};

	// 重写onCreat方法,来显示自己的视图
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 将用xml文件写好的listview视图进行填充
		setContentView(R.layout.custom_listview);

		// 将设计好的组件添加到ListView当中
		lv = (ListView) findViewById(R.id.actList);
		lv.setAdapter(adapter);
	}
}


创建用于显示主布局中的小组件的类

package com.person.listviewtest;

public class CustomListCellData {

	private String name;
	private String description;
	int iconId = 0;

	public CustomListCellData(String name, String description, int iconId) {
		this.setName(name);
		this.setDescription(description);
		this.iconId = iconId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}

主布局的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="vertical" >

    <ListView
        android:id="@+id/actList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

用于填充的主页面中的ListView的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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="description" />
    </LinearLayout>

</LinearLayout>


最后设置mannifest.XML中的activity用于启动主程序,将自己写好的ListVIew显示动作进行注册


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.person.listviewtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.person.listviewtest.CustomListView"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在XML文件中的 layout_weight的意义是将原来的父容器进行等分之后进行填充,在上述的小组件中的XML中的layout_weight是将剩余的空间进行填充


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值