国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout

2019-10-23 18:29:20
字體:
來源:轉載
供稿:網友

android,gridview單選

思路:

外層控件用的是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就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰州市| 吉林省| 南安市| 封开县| 德江县| 安国市| 安泽县| 涡阳县| 沙田区| 承德市| 文水县| 开鲁县| 望谟县| 株洲县| 安义县| 武义县| 综艺| 工布江达县| 武功县| 宝丰县| 河津市| 美姑县| 长武县| 太保市| 清水县| 安仁县| 彭阳县| 肇东市| 钦州市| 克拉玛依市| 治多县| 闻喜县| 突泉县| 磴口县| 金平| 彝良县| 新郑市| 万荣县| 景德镇市| 湖北省| 长白|