詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
思路:
外層控件用的是GridView,里面每個(gè)item放一個(gè)FrameLayout,F(xiàn)rameLayout里面有Checkbox和ImageView,chechBox添加background實(shí)現(xiàn)選中效果,選中背景為透明,顯示item的勾勾圖標(biāo),不選中checkbox就有背景,擋住選中的勾勾。。重寫(xiě)GridView,實(shí)現(xiàn)監(jiān)聽(tīng)和數(shù)據(jù)適配,用一個(gè)接口返回選中的數(shù)據(jù)。
代碼:
ChooseMoneyLayout.java
public class ChooseMoneyLayout extends GridView { private int[] moneyList = {}; //數(shù)據(jù)源 private LayoutInflater mInflater; private MyAdapter adapter; //適配器 int defaultChoose = 0; //默認(rèn)選中項(xiàng) public ChooseMoneyLayout(Context context, AttributeSet attrs) { super(context, attrs); setData(); } public void setData() { mInflater = LayoutInflater.from(getContext()); //配置適配器 adapter = new MyAdapter(); setAdapter(adapter); } /** * 設(shè)置默認(rèn)選擇項(xiàng)目, * @param defaultChoose */ public void setDefaultPositon(int defaultChoose) { this.defaultChoose = defaultChoose; adapter.notifyDataSetChanged(); } /** * 設(shè)置數(shù)據(jù)源 * @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) { //設(shè)置選中文字顏色 buttonView.setTextColor(getResources().getColor(R.color.light_color_blue)); //取消上一個(gè)選擇 if (checkBox != null) { checkBox.setChecked(false); } checkBox = (CheckBox) buttonView; } else { checkBox = null; //設(shè)置不選中文字顏色 buttonView.setTextColor(getResources().getColor(R.color.darkgray)); } //回調(diào) 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 錢(qián)數(shù) */ 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"> <!-- 選中時(shí)候的圖片 --> <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; //當(dāng)前選擇的金額 private void initData() { //獲取控件 moneyChoseMoney = (ChooseMoneyLayout)findViewById(R.id.money_chose_money); //數(shù)設(shè)置據(jù)源 moneyChoseMoney.setMoneyData(new int[]{30, 50, 100, 200, 300, 500,1000}); //設(shè)置默認(rèn)選中項(xiàng) moneyChoseMoney.setDefaultPositon(3); //金額選擇監(jiān)聽(tīng) 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就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android模擬強(qiáng)制下線通知功能實(shí)例代碼
這篇文章主要介紹了Android模擬強(qiáng)制下線通知功能實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android數(shù)據(jù)加密之Base64編碼算法的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇Android數(shù)據(jù)加密之Base64編碼算法的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10Android判斷應(yīng)用程序退到后臺(tái)的方法(示例代碼)
判斷手機(jī)是否退到后臺(tái),這是我們?cè)贏ndroid開(kāi)發(fā)中實(shí)現(xiàn)一些功能時(shí),經(jīng)常會(huì)考慮的問(wèn)題,這篇文章主要介紹了android判斷應(yīng)用程序退到后臺(tái)的方法,需要的朋友可以參考下2023-03-03Android 判斷所有字段是否已經(jīng)輸入的實(shí)例
今天小編就為大家分享一篇Android 判斷所有字段是否已經(jīng)輸入的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android自定義TextView實(shí)現(xiàn)文字圖片居中顯示的方法
下面小編就為大家分享一篇Android自定義TextView實(shí)現(xiàn)文字圖片居中顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android ListView在Fragment中的使用示例詳解
這篇文章主要介紹了Android ListView在Fragment中的使用,因?yàn)楣ぷ饕恢痹谟胢vvm框架,因此這篇文章是基于mvvm框架寫(xiě)的,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Flutter 系統(tǒng)是如何實(shí)現(xiàn)ExpansionPanelList的示例代碼
Flutter組件有一個(gè)很大的特色,那就是很多復(fù)雜的組件都是通過(guò)一個(gè)一個(gè)小組件拼裝而成的,今天就來(lái)說(shuō)說(shuō)系統(tǒng)的ExpansionPanelList是如何實(shí)現(xiàn)的,需要的朋友可以參考下2020-05-05Android實(shí)現(xiàn)雷達(dá)View效果的示例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)雷達(dá)View效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06