思路:
外层控件用的是GrIDVIEw,里面每个item放一个FrameLayout,FrameLayout里面有CheckBox和ImageVIEw,chechBox添加background实现选中效果,选中背景为透明,显示item的勾勾图标,不选中checkBox就有背景,挡住选中的勾勾。。重写GrIDVIEw,实现监听和数据适配,用一个接口返回选中的数据。
代码:
ChooseMoneyLayout.java
public class ChooseMoneyLayout extends GrIDVIEw { private int[] moneyList = {}; //数据源 private LayoutInflater mInflater; private MyAdapter adapter; //适配器 int defaultChoose = 0; //默认选中项 public ChooseMoneyLayout(Context context,AttributeSet attrs) { super(context,attrs); setData(); } public voID setData() { mInflater = LayoutInflater.from(getContext()); //配置适配器 adapter = new MyAdapter(); setAdapter(adapter); } /** * 设置默认选择项目, * @param defaultChoose */ public voID setDefaultPositon(int defaultChoose) { this.defaultChoose = defaultChoose; adapter.notifyDataSetChanged(); } /** * 设置数据源 * @param moneyData */ public voID setMoneyData(int[] moneyData){ this.moneyList = moneyData; } class MyAdapter extends BaseAdapter { private CheckBox checkBox; @OverrIDe public int getCount() { return moneyList.length; } @OverrIDe public Object getItem(int position) { return moneyList[position]; } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) { MyVIEwHolder holder; if (convertVIEw == null) { holder = new MyVIEwHolder(); convertVIEw = mInflater.inflate(R.layout.item_money_pay,parent,false); holder.moneyPayCb = (CheckBox) convertVIEw.findVIEwByID(R.ID.money_pay_cb); convertVIEw.setTag(holder); } else { holder = (MyVIEwHolder) convertVIEw.getTag(); } holder.moneyPayCb.setText(getItem(position) + "元"); holder.moneyPayCb.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) { if (isChecked) { //设置选中文字颜色 buttonVIEw.setTextcolor(getResources().getcolor(R.color.light_color_blue)); //取消上一个选择 if (checkBox != null) { checkBox.setChecked(false); } checkBox = (CheckBox) buttonVIEw; } else { checkBox = null; //设置不选中文字颜色 buttonVIEw.setTextcolor(getResources().getcolor(R.color.darkgray)); } //回调 Listener.chooseMoney(position,isChecked,(Integer) getItem(position)); } }); if (position == defaultChoose) { defaultChoose = -1; holder.moneyPayCb.setChecked(true); checkBox = holder.moneyPayCb; } return convertVIEw; } private class MyVIEwHolder { private CheckBox moneyPayCb; } } /** * 解决嵌套显示不完 * @param wIDthMeasureSpec * @param heightmeasureSpec */ @OverrIDe public voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(wIDthMeasureSpec,expandSpec); } private onChoseMoneyListener Listener; public voID setonChoseMoneyListener(onChoseMoneyListener Listener) { this.Listener = Listener; } public interface onChoseMoneyListener { /** * 选择金额返回 * * @param position grIDVIEw的位置 * @param isCheck 是否选中 * @param moneyNum 钱数 */ voID chooseMoney(int position,boolean isCheck,int moneyNum); }}
item_money_pay.xml
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="80dp" androID:descendantFocusability="blocksDescendants"> <!-- 选中时候的图片 --> <ImageVIEw androID:layout_wIDth="15dp" androID:layout_height="15dp" androID:layout_gravity="right|bottom" androID:layout_marginBottom="3dp" androID:layout_marginRight="3dp" androID:maxHeight="9dp" androID:maxWIDth="9dp" androID:scaleType="fitCenter" androID:src="@drawable/money_pay_type_choose" /> <CheckBox androID:ID="@+ID/money_pay_cb" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_gravity="center" androID:background="@drawable/money_pay_selector" androID:button="@null" androID:gravity="center" androID:paddingBottom="2.5dp" androID:paddingleft="15dp" androID:paddingRight="15dp" androID:paddingtop="2.5dp" androID:textSize="20sp" androID:textcolor="#ff777777" /> </FrameLayout>
CheckBox的background: money_pay_selector.xml
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_pressed="true" androID:drawable="@drawable/blue_border_noback_drawable"/> <item androID:state_selected="true" androID:drawable="@drawable/blue_border_noback_drawable"/> <item androID:state_checked="true" androID:drawable="@drawable/blue_border_noback_drawable"/> <item > <shape> <solID androID:color="#ffffffff"/> <corners androID:radius="5dp"/> <stroke androID:color="#ffbfbfbf" androID:wIDth="1dp"/> </shape> </item> </selector>
activity xml:
<com.minstone.vIEw.ChooseMoneyLayout androID:ID="@+ID/money_chose_money" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="10dp" androID:horizontalSpacing="17dp" androID:numColumns="3" androID:verticalSpacing="20dp" />
activity里面代码:
private ChooseMoneyLayout moneyChoseMoney; private int money; //当前选择的金额 private voID initData() { //获取控件 moneyChoseMoney = (ChooseMoneyLayout)findVIEwByID(R.ID.money_chose_money); //数设置据源 moneyChoseMoney.setMoneyData(new int[]{30,50,100,200,300,500,1000}); //设置默认选中项 moneyChoseMoney.setDefaultPositon(3); //金额选择监听 moneyChoseMoney.setonChoseMoneyListener(new ChooseMoneyLayout.onChoseMoneyListener() { @OverrIDe public voID chooseMoney(int position,int moneyNum) { if(isCheck){ money = moneyNum; ToastUtil.showCustomToast(PayActivity.this,money+""); }else{ money = 0; } } }); }
以上这篇详谈自定义view之GrIDVIEw单选 金额选择Layout-ChooseMoneyLayout就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout全部内容,希望文章能够帮你解决详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)