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

所属分类: 软件编程 / Android 阅读数: 51
收藏 0 赞 0 分享

思路:

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

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

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多