欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-03 10:23:04
思路: 外层控件用的是gridview,里面每个item放一个framelayout,framelayout里面有checkbox和imageview,chechbo...

详谈自定义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);
  }
 
  /**
   * 设置默认选择项目,
   * @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就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。