栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout

思路:

外层控件用的是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);
  }
 
  
  public void setDefaultPositon(int defaultChoose) {
    this.defaultChoose = defaultChoose;
    adapter.notifyDataSetChanged();
  }
 
  
  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;
    }
  }
 
 
  
  @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 {
    
    void chooseMoney(int position, boolean isCheck, int moneyNum);
  }
}

item_money_pay.xml

 



 

  
 
  
 

 CheckBox的background: money_pay_selector.xml



 
  
  
  
 
  
 
    
      
      
      
    
 
  
 

activity xml:

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,boolean isCheck, int moneyNum) {
 if(isCheck){
   money = moneyNum;
   ToastUtil.showCustomToast(PayActivity.this,money+"");
 }else{
   money = 0;
 }
      }
    });
 
  }

以上这篇详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号