试试这个解决方案
package com.example.demo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
public class ListViewDemo extends Activity {
ListView LSOne;
int[] _intRadio = new int[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_radio);
LSOne = (ListView) findViewById(R.id.LSOne);
LSOne.setAdapter(new LsAdapter());
}
public class LsAdapter extends BaseAdapter {
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 20;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
int _intPosition = getItemViewType(position);
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_radio_raw, null);
holder = new ViewHolder();
holder.code = (TextView) convertView
.findViewById(R.id.textView1);
holder.name = (RadioButton) convertView
.findViewById(R.id.radioButton1);
convertView.setTag(holder);
holder.name.setId(_intPosition);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < _intRadio.length; i++) {
if (i == v.getId()) {
_intRadio[i] = 1;
} else {
_intRadio[i] = 0;
}
}
notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
if (_intRadio[_intPosition] == 1) {
holder.name.setChecked(true);
} else {
holder.name.setChecked(false);
}
return convertView;
}
private class ViewHolder {
TextView code;
RadioButton name;
Button btn;
}
}
}
listview_radio_raw.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dip"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/button_radio"
android:paddingLeft="60dip"
android:text="RadioButton" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
button_radio.xml
//这个xml添加到你的drawable文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="false"/>
<item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="false"/>
<item android:drawable="@drawable/radio_on_pressed" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/radio_off_pressed" android:state_checked="false" android:state_pressed="true"/>
</selector>
下载此链接的所有图片