listview含有radiobtn,点击实现单选

注意事项:
首先自定义ChoiceView类实现Checkable接口

public class ChoiceView extends FrameLayout implements Checkable{

    private TextView mTextView;
    public RadioButton mRadioButton;

    public ChoiceView(Context context) {
        super(context);
        View.inflate(context, R.layout.listview_installment_item, this);
        mTextView = (TextView) findViewById(R.id.tv_qishu);
        mRadioButton = (RadioButton) findViewById(R.id.radiobtn);
    }

    public void setText(String text) {
        mTextView.setText(text);
    }

    @Override
    public void setChecked(boolean checked) {
        mRadioButton.setChecked(checked);
    }

    @Override
    public boolean isChecked() {
        return mRadioButton.isChecked();
    }

    @Override
    public void toggle() {
        mRadioButton.toggle();
    }
}

单选item的布局listview_installment_item,我的布局代码用了弘阳的AutoRelativeLayout库,自己可以修改成安卓自己的RelativeLayout,另外需要注意的是radiobutton的android:clickable=”false”
android:focusable=”false”
android:focusableInTouchMode=”false”
这三个属性必须加,要不会报错

"1.0" encoding="utf-8"?>
<com.lixue.aibei.autolayoutlib.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="match_parent">
<com.lixue.aibei.autolayoutlib.AutoRelativeLayout
    android:layout_width="match_parent"
    android:layout_marginLeft="20px"
    android:layout_marginRight="20px"
    android:layout_height="80px">
    "@+id/tv_qishu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_centerVertical="true"
        android:text="3期"
       android:textColor="@color/text_gray"
        />
    "@+id/radiobtn"
        android:layout_width="54px"
        android:layout_height="54px"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:button="@drawable/selector_radiobtn"
        />
com.lixue.aibei.autolayoutlib.AutoRelativeLayout>
com.lixue.aibei.autolayoutlib.AutoLinearLayout>

在activty的oncreate中,写如下代码

   for(int i=0;i<5;i++){
                    PeriodandRateBean periodandRateBean=new PeriodandRateBean();
                    periodandRateBean.setLimit(i+"");
                    periodandRateBeanList.add(periodandRateBean);
                }
                ListAdapter adapter = new ArrayAdapter(SocialsecurityLoanActivity.this, R.layout.listview_installment_item, periodandRateBeanList) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        final ChoiceView view;
                        if(convertView == null) {
                            view = new ChoiceView(SocialsecurityLoanActivity.this);
                        } else {
                            view = (ChoiceView)convertView;
                        }
                        view.setText(periodandRateBeanList.get(position).getLimit()+"个月");
                        return view;
                    }
                };
                   //这个属性可以设置默认选中的项
               listView.setItemChecked(fenqi_select_pos, true);

PeriodandRateBean 这个bean是自己定义的,依据自己的需求来写,我的效果最后实现的是这样,这里参考了稀土掘金大神的帖子,大家可以去看一下

listview含有radiobtn,点击实现单选_第1张图片

你可能感兴趣的:(android开发)