重写ListView实现RaidoButton重写ListView实现RaidoButton单选改进版单选改进版
前言:之前做的ListView实现RadioButton的功能有bug,当ListView控件的内容超出屏幕可见区域时,滑动ListView控件会报错,下面有为什么出错和解决方法进行的注解,不多说了,看源码,有更好的解决办法请指教
1,MainActivity.java
package com.excetop.listradio;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
private String[] s = new String[]{"a","b","c","d","e","a","b","c","d","e","a","b","c","d","e","a","b","c","d","e"};
private int temp = -1;
@Override
public int getCount() {
// TODO Auto-generated method stub
return s.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null); //解决办法: 每次都重新获取View
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(s[position]);
RadioButton radioButton = (RadioButton) convertView.findViewById(R.id.radioButton);
radioButton.setId(position); //把position设为radioButton的id
radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//这段代码来实现单选功能
if(temp != -1){
RadioButton tempButton = (RadioButton) MainActivity.this.findViewById(temp);
if(tempButton != null){
tempButton.setChecked(false);
}
}
temp = buttonView.getId();
Log.i(TAG,"you are women- - " + isChecked + " " + temp);
}
}
});
//这里实现单选框选的回显,解决了单选框移出屏幕范围未选中状态
if(temp == position){
radioButton.setChecked(true);
}
return convertView;
}
// Holder holder;
// if(convertView == null){ //1,当第一次加载ListView控件时 convertView为空
// convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null); //所以当ListView控件没有滑动时都会执行这条语句
// holder = new Holder();
// convertView.setTag(holder);
// }else{
// holder = (Holder) convertView.getTag();
// }
//
// holder.button = (Button) convertView.findViewById(R.id.button);
// holder.button.setText(s[position]);
//
// holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton); //
// holder.radioButton.setId(position); //2,因为这里对radioButton的id进行重新设置,滑动ListView时convertView不为空,上面的语句就没法得到radioButton对象,这条语句就会报空指针异常
// holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
// if(isChecked){
// if(temp != -1){
// RadioButton tempButton = (RadioButton) MainActivity.this.findViewById(temp);
// tempButton.setChecked(false);
//
// }
//
// temp = buttonView.getId();
// Log.i(TAG,"you are women- - " + isChecked + " " + temp);
//
// }
// }
// });
// return convertView;
// }
// private class Holder{
// private Button button;
// private RadioButton radioButton;
// }
}
}
2,item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试">
</Button>
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
出处:http://user.qzone.qq.com/334978366/infocenter
posted on 2011-05-31 11:06 neil-zhao 阅读(1395) 评论(0) 编辑 收藏 举报