Android RadioButton与ListView的混合使用

布局文件很简单:
   
    <LinearLayout  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" >

   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/select_authors"
        android:textSize="25sp" />

   
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" />





ItemList也不复杂:
   
    
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   
        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"/>

   
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>



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;
}
}
最后看看效果图:
Android RadioButton与ListView的混合使用_第1张图片

Android RadioButton与ListView的混合使用_第2张图片
代码链接:http://vdisk.weibo.com/s/d-PE6ROK-NCF

你可能感兴趣的:(Android RadioButton与ListView的混合使用)