listview含有radiobtn,点击实现单选
2016-09-23 17:44
555 查看
注意事项:
首先自定义ChoiceView类实现Checkable接口
单选item的布局listview_installment_item,我的布局代码用了弘阳的AutoRelativeLayout库,自己可以修改成安卓自己的RelativeLayout,另外需要注意的是radiobutton的android:clickable=”false”
android:focusable=”false”
android:focusableInTouchMode=”false”
这三个属性必须加,要不会报错
在activty的oncreate中,写如下代码
PeriodandRateBean 这个bean是自己定义的,依据自己的需求来写,我的效果最后实现的是这样,这里参考了稀土掘金大神的帖子,大家可以去看一下
首先自定义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”
这三个属性必须加,要不会报错
<?xml version="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"> <TextView android:id="@+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" /> <RadioButton android:id="@+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<PeriodandRateBean>(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是自己定义的,依据自己的需求来写,我的效果最后实现的是这样,这里参考了稀土掘金大神的帖子,大家可以去看一下
相关文章推荐
- html中点击radio后的文字相当于选中该单选按钮如何实现?怎么实现两个redio,选中一个,取消另一个?
- Android ListView中点击单行实现RadioButton的单选功能,自定义Item布局文件
- python+selenium 点击单选框-radio的实现
- 小白求教!listview item 含有button 点击事件怎么实现?
- Listview+RadioButton实现单选
- Android ListView+RadioButton实现单选的方法
- ListView+radioButton实现每行单选
- 在C#中实现对ListView点击列标题自动排序功能
- 利用radio实现Datagrid的单选
- C# ListView实现点击列表头实现排序
- 在C#中实现对ListView点击列标题自动排序功能
- 在DataGrid中添加Radio实现单选
- 点击单选按钮实现页面跳转
- 在C#中实现对ListView点击列标题自动排序功能
- 点击文字实现radio
- 重写ListView控件,实现点击列头排序的功能
- 在C#中实现对ListView点击列标题自动排序功能
- 在C#中实现对ListView点击列标题自动排序功能
- 利用radio实现Datagrid的单选
- 利用radio实现Datagrid的单选