布局文件简单,重要的就是设置radiobutton的属性 android:clickable="false" 和 android:focusable="false"
<?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="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/item_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"/>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项item"/>
</LinearLayout>
main.xml
<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" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#FFFFFF"
android:divider="@null"></ListView>
</RelativeLayout>
代码:
package com.radio;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView listView;
private String[] list = {"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",
"item11111111","item2222222","item33333333","item444444",};
private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listview);
final RadioAdapter adapter = new RadioAdapter(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
map.clear();
map.put(arg2, 100);
adapter.notifyDataSetChanged();
}
});
}
class RadioHolder{
private RadioButton radio;
private TextView item;
public RadioHolder(View view){
this.radio = (RadioButton) view.findViewById(R.id.item_radio);
this.item = (TextView) view.findViewById(R.id.item_text);
}
}
class RadioAdapter extends BaseAdapter{
private Context context;
public RadioAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list[arg0];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
RadioHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(
R.layout.item, null);
holder = new RadioHolder(convertView);
convertView.setTag(holder);
}else{
holder = (RadioHolder) convertView.getTag();
}
holder.radio.setChecked(map.get(position) == null ? false : true);
holder.item.setText(list[position]);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}