listview含有radiobtn,点击实现单选
来源:互联网 发布:网络研修收获与反思 编辑:程序博客网 时间:2024/06/01 12:21
注意事项:
首先自定义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是自己定义的,依据自己的需求来写,我的效果最后实现的是这样,这里参考了稀土掘金大神的帖子,大家可以去看一下
0 0
- listview含有radiobtn,点击实现单选
- listview实现单选
- radiobtn的listview,自定义arrayAdapter构成样式
- Android ListView 点击单选State
- 含有Button的ListView不能点击问题
- Android ListView中点击单行实现RadioButton的单选功能,自定义Item布局文件
- 小白求教!listview item 含有button 点击事件怎么实现?
- ListView实现RaidoButton单选
- ListView实现单选(一)
- 实现listview单选效果
- listview+checkbox实现单选
- Listview+RadioButton实现单选
- android listView实现单选
- ListView实现单选功能
- Android ListView实现单选
- listview、GridView单点击效果,点击改变背景改变item字体颜色实现
- listview + 单选按钮 有时候会点击出现重复多次
- listview实现点击选中
- mysql innodb checkpoint
- iOS -- 关于状态栏
- 无向最小生成树(prim 与krusckal)总结
- 个人对移动App的看法
- Some urls - Web relating
- listview含有radiobtn,点击实现单选
- WIFI的Log切入点
- 2017届毕业生安卓面试题-JAVA篇
- *****************************自己总结的小技巧**********************************
- 24 AIDL案例
- hd 1065 I Think I Need a Houseboat(数学、水)
- Python代理+线程+进程+socket
- HDU 5381The sum of gcd 莫队算法
- 可变参数【Java】