许久没有写过博客了,近来在做Android开发,突然想到这里,以后可以在这里贴些代码,做些记录,与大家分享交流。Android开发中,常常会用到RadioButton与ListView的混合使用,用户点击一条Item,然后记录下选中的状态,其中最重要的是记录好用户点击选中Item的位置。
布局文件很简单:
<LinearLayout xmlns: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:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/select_authors" android:textSize="25sp" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" /> </LinearLayout>
ItemList也不复杂:
<RelativeLayout xmlns: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"> <TextView android:id="@+id/author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="@string/author_name" android:textSize="20sp"/> <RadioButton android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true"/> </RelativeLayout>
RadioButtonList 类,这里我写了一些自己喜欢的作家(当然我也很喜欢和大家交流一些文学作品)作为模拟数据。
package com.example.radiobuttonlisttest; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class RadioButtonList 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_radio_button_list); radioButtonList = (ListView) findViewById(R.id.list); adapter = new RadioAdapter(this, authors); radioButtonList.setAdapter(adapter); } }
适配器是最关键的,标记好选择的位置,选中状态不会因为ListView的滑动而出现混乱:
package com.example.radiobuttonlisttest; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; public class RadioAdapter extends BaseAdapter { private LayoutInflater inflater; private String[] authors; private viewHolder holder; // 标记用户当前选择的那一个作家 private int index = -1; private Context c; public RadioAdapter(Context c, String[] authors) { super(); this.c = c; this.authors = authors; inflater = LayoutInflater.from(c); } @Override public int getCount() { return authors.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { holder = new viewHolder(); if (convertView == null) { convertView = inflater.inflate(R.layout.item_list, null); holder.nameTxt = (TextView) convertView.findViewById(R.id.author); holder.selectBtn = (RadioButton) convertView .findViewById(R.id.radio); convertView.setTag(holder); } else { holder = (viewHolder) convertView.getTag(); } holder.nameTxt.setText(authors[position]); holder.selectBtn .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(c, "您选择的作家是:" + authors[position], Toast.LENGTH_LONG).show(); index = position; notifyDataSetChanged(); } } }); if (index == position) {// 选中的条目和当前的条目是否相等 holder.selectBtn.setChecked(true); } else { holder.selectBtn.setChecked(false); } return convertView; } public class viewHolder { public TextView nameTxt; public RadioButton selectBtn; } }
最后看看效果图:
时间: 2024-12-25 20:29:25