ListView 的单选模式
《RadioButton与ListView的混合使用》一文中,我在适配器中用标记的方法实现了用户选择的操作,这次用ListView的单选模式来实现一下。ListView的默认状态下是没有选择行为的,把ListView的choiceMode设置为singleChoice,列表就可以实现单选(当然它也有多选模式,这个后面再研究)。
Activity的布局文件如下,ListView选择了单选模式,这次我把ListView上方的TextView换成了Button:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/select"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="@string/select_authors"android:textSize="25sp"/><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"android:choiceMode="singleChoice"/></LinearLayout>
ItemList的XML文件,RadioButton换成了CheckBox,另外,CheckBox 是可以获取焦点的UI控件,为实现ListView的点击,需要设置
“android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"”
这三项,其中,CheckBox的背景选用了自己做的一张图片,图片是RadioButton的样子:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="#fff"><TextViewandroid:id="@+id/author"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:padding="10dp"android:textSize="20sp"/><CheckBoxandroid:id="@+id/radio"android:layout_width="20dp"android:layout_height="20dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_gravity="center_vertical"android:layout_marginRight="10dp"android:background="@drawable/radio_button_normal"android:button="@null"android:clickable="false"android:focusable="false"android:focusableInTouchMode="false"android:padding="10dp"/></RelativeLayout>
Activity的代码如下,点击ListView的Item或者其上方的Button,都可以弹出Toast:
packagecom.example.choicelistviewtest;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.AdapterView;importandroid.widget.Toast;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.ListView;publicclassRadioButtonListActivityextendsActivity{privateListViewradioButtonList;privateRadioAdapteradapter;//模拟几个数据,作为List的条目privateString[]authors={"芥川龙之介","三岛由纪夫","川端康成","村上春树","东野圭吾","张爱玲","金庸","钱钟书","老舍","梁实秋","亨利米勒","海明威","菲兹杰拉德","凯鲁亚克","杰克伦敦","小仲马","杜拉斯","福楼拜","雨果","巴尔扎克","莎士比亚","劳伦斯","毛姆","柯南道尔","笛福"};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_choice_list_view_test);radioButtonList=(ListView)findViewById(R.id.list);adapter=newRadioAdapter(this,authors);radioButtonList.setAdapter(adapter);radioButtonList.setOnItemClickListener(newOnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){Toast.makeText(RadioButtonListActivity.this,"您选择的作家是:"+authors[arg2],Toast.LENGTH_SHORT).show();}});findViewById(R.id.select).setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){intselect=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();}}});}}
适配器:
packagecom.example.choicelistviewtest;importandroid.content.Context;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;publicclassRadioAdapterextendsBaseAdapter{privateString[]authors;privateContextc;publicRadioAdapter(Contextc,String[]authors){super();this.c=c;this.authors=authors;}@OverridepublicintgetCount(){returnauthors.length;}@OverridepublicObjectgetItem(intarg0){returnnull;}@OverridepubliclonggetItemId(intarg0){return0;}@OverridepublicViewgetView(intarg0,Viewarg1,ViewGrouparg2){ChoiceListItemViewchoiceListItemView=newChoiceListItemView(c,null);choiceListItemView.setName(authors[arg0]);returnchoiceListItemView;}}
ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该接口,ListView选中某个Item时,会调用ChoiceListItemView类的setChecked的方法:
packagecom.example.choicelistviewtest;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.widget.CheckBox;importandroid.widget.Checkable;importandroid.widget.LinearLayout;importandroid.widget.TextView;publicclassChoiceListItemViewextendsLinearLayoutimplementsCheckable{privateTextViewnameTxt;privateCheckBoxselectBtn;publicChoiceListItemView(Contextcontext,AttributeSetattrs){super(context,attrs);LayoutInflaterinflater=LayoutInflater.from(context);Viewv=inflater.inflate(R.layout.item_list,this,true);nameTxt=(TextView)v.findViewById(R.id.author);selectBtn=(CheckBox)v.findViewById(R.id.radio);}publicvoidsetName(Stringtext){nameTxt.setText(text);}@OverridepublicbooleanisChecked(){returnselectBtn.isChecked();}@OverridepublicvoidsetChecked(booleanchecked){selectBtn.setChecked(checked);//根据是否选中来选择不同的背景图片if(checked){selectBtn.setBackgroundResource(R.drawable.radio_button_checked);}else{selectBtn.setBackgroundResource(R.drawable.radio_button_normal);}}@Overridepublicvoidtoggle(){selectBtn.toggle();}}
效果图:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。