135-1821-9792

ListView的单选模式

    《RadioButton与ListView的混合使用》一文中,我在适配器中用标记的方法实现了用户选择的操作,这次用ListView的单选模式来实现一下。ListView的默认状态下是没有选择行为的,把ListView的choiceMode设置为singleChoice,列表就可以实现单选(当然它也有多选模式,这个后面再研究)。

成都创新互联公司长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为东阳企业提供专业的网站建设、做网站东阳网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

    Activity的布局文件如下,ListView选择了单选模式,这次我把ListView上方的TextView换成了Button:



    

    ItemList的XML文件,RadioButton换成了CheckBox,另外, CheckBox 是可以获取焦点的UI控件,为实现ListView的点击,需要设置

“  android:clickable="false"

    android:focusable="false"

    android:focusableInTouchMode="false"”

这三项,其中,CheckBox的背景选用了自己做的一张图片,图片是RadioButton的样子:



    

    

    Activity的代码如下,点击ListView的Item或者其上方的Button,都可以弹出Toast:

package com.example.choicelistviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class RadioButtonListActivity extends Activity {

	private ListView radioButtonList;
	private RadioAdapter adapter;
	// 模拟几个数据,作为List的条目
	private String[] authors = { "芥川龙之介", "三岛由纪夫", "川端康成", "村上春树", "东野圭吾",
			"张爱玲", "金庸", "钱钟书", "老舍", "梁实秋", "亨利米勒", "海明威", "菲兹杰拉德", "凯鲁亚克",
			"杰克伦敦", "小仲马", "杜拉斯", "福楼拜", "雨果", "巴尔扎克", "莎士比亚", "劳伦斯", "毛姆",
			"柯南道尔", "笛福" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choice_list_view_test);
		radioButtonList = (ListView) findViewById(R.id.list);
		adapter = new RadioAdapter(this, authors);
		radioButtonList.setAdapter(adapter);
		radioButtonList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {

				Toast.makeText(RadioButtonListActivity.this,
						"您选择的作家是:" + authors[arg2], Toast.LENGTH_SHORT).show();
			}
		});

		findViewById(R.id.select).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int select = radioButtonList.getCheckedItemPosition();
				// INVALID_POSITION 代表无效的位置。有效值的范围是 0 到当前适配器项目数减 1 。
				if (ListView.INVALID_POSITION != select) {
					Toast.makeText(RadioButtonListActivity.this,
							"您选择的作家是:" + authors[select], Toast.LENGTH_SHORT)
							.show();
				} else {
					// 如果用户开始没有选择
					Toast.makeText(RadioButtonListActivity.this, "请选择一位作家!",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}

    适配器:

package com.example.choicelistviewtest;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class RadioAdapter extends BaseAdapter {

	private String[] authors;
	private Context c;

	public RadioAdapter(Context c, String[] authors) {
		super();
		this.c = c;
		this.authors = authors;
	}

	@Override
	public int getCount() {
		return authors.length;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		ChoiceListItemView choiceListItemView = new ChoiceListItemView(c, null);
		choiceListItemView.setName(authors[arg0]);
		return choiceListItemView;
	}

}

    ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该接口,ListView选中某个Item时,会调用ChoiceListItemView类的setChecked的方法:

package com.example.choicelistviewtest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChoiceListItemView extends LinearLayout implements Checkable {

	private TextView nameTxt;
	private CheckBox selectBtn;
	public ChoiceListItemView(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.item_list, this, true);
		nameTxt = (TextView) v.findViewById(R.id.author);
		selectBtn = (CheckBox) v.findViewById(R.id.radio);
	}

	public void setName(String text) {
		nameTxt.setText(text);
	}

	@Override
	public boolean isChecked() {
		return selectBtn.isChecked();
	}

	@Override
	public void setChecked(boolean checked) {
		selectBtn.setChecked(checked);
		//根据是否选中来选择不同的背景图片
		if (checked) {
			selectBtn.setBackgroundResource(R.drawable.radio_button_checked);
		} else {
			selectBtn.setBackgroundResource(R.drawable.radio_button_normal);
		}
	}

	@Override
	public void toggle() {
		selectBtn.toggle();
	}

}

    效果图:

ListView 的单选模式


分享文章:ListView的单选模式
标题网址:http://kswsj.com/article/jhiice.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号