06-android入门_listview采用系统和自定义的方式实现item

本文介绍在Android开发中如何使用ListView实现单选和多选功能,并提供了两种布局实现方案:一是利用系统的简单列表项布局,二是通过自定义布局实现。

1、环境的搭建

2、方式一:系统的listView的布局的实现

首先在activity中的实现为:

// 单选
		private View singleChoice(int position) {
			// 获取当前位置显示的对象
			User entity = entities.get(position);
			// 创建出每个条目的控件对象
			View v = View.inflate(MainActivity.this,
					android.R.layout.simple_list_item_single_choice, null);
			CheckedTextView tv = (CheckedTextView) v
					.findViewById(android.R.id.text1);
			// CheckBox tv=new CheckBox(MainActivity.this);
			// TextView tv=new TextView(MainActivity.this);
			tv.setText(entity.getUserName() + "-" + entity.getUserPhone());
			tv.setHeight(40);
			return v;
		}

		// 多选
		private View multipleChoice(int position) {
			// 获取当前位置显示的对象
			User entity = entities.get(position);
			// 创建出每个条目的控件对象
			View v = View.inflate(MainActivity.this,
					android.R.layout.simple_list_item_multiple_choice, null);
			CheckedTextView tv = (CheckedTextView) v
					.findViewById(android.R.id.text1);
			// CheckBox tv=new CheckBox(MainActivity.this);
			// TextView tv=new TextView(MainActivity.this);
			tv.setText(entity.getUserName() + "-" + entity.getUserPhone());
			tv.setHeight(40);
			return v;
		}


在此同时,listView中的choiceMode的值也要作相应的改变(但是与效果无关):

<ListView
        android:id="@+id/lv_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:divider="#FF0000"
        android:dividerHeight="2dp"
        android:choiceMode="multipleChoice"
        android:layout_alignParentTop="true" >
    </ListView>

实现时,只需要在getView()中实现:

return multipleChoice(position);
3、方式二:自定义的listView的布局

首先,在activity中的实现为:

// 返回每个条目
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 系统定义的item布局
			// 注意:对应的布局中choiceMode也要改变
			// return singleChoice(position);
			// return multipleChoice(position);

			// 自定义item布局
			User entity = entities.get(position);
			View v = View.inflate(MainActivity.this, R.layout.list_item_user,
					null);
			TextView tv_id = (TextView) v.findViewById(R.id.tv_id);
			TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
			TextView tv_age = (TextView) v.findViewById(R.id.tv_age);
			TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);

			tv_id.setText("" + entity.getUserId());
			tv_name.setText("姓名:" + entity.getUserName());
			tv_age.setText("年龄:" + entity.getUserAge());
			tv_phone.setText("电话:" + entity.getUserPhone());

			return v;
		}


在此之前,要新建一个list_item_user.xml来实现item中的布局:

<TextView
        android:id="@+id/tv_id"
        android:layout_width="39dp"
        android:layout_height="56dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:text="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="56dp"
        android:layout_y="4dp"
        android:text="姓名:zs" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="56dp"
        android:layout_y="25dp"
        android:text="年龄:22" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="173dp"
        android:layout_height="31dp"
        android:layout_x="129dp"
        android:layout_y="10dp"
        android:text="电话:12345678765" />


4、实现的结果为:

方式一:

方式二:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值