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);
}
}
<?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>
<?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>
<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" />
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,boolean isCheck, int moneyNum) {
if(isCheck){
money = moneyNum;
ToastUtil.showCustomToast(PayActivity.this,money+"");
}else{
money = 0;
}
}
});
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有