Select Layout-ChooseMoneyLayout for the custom View's GridView single-choice amount.

Source: Internet
Author: User
Tags getcolor

Select Layout-ChooseMoneyLayout for the custom View's GridView single-choice amount.

Ideas:

The outer control uses the GridView, where each item has a FrameLayout, And the FrameLayout contains the Checkbox and ImageView. The background is added to the chechBox to achieve the selected effect. The selected background is transparent and the check icon of the item is displayed, if the checkbox is not selected, a background is displayed, which blocks the checked check .. Rewrite the GridView to adapt the listener to the data, and use an interface to return the selected data.

Code:

ChooseMoneyLayout. java

Public class ChooseMoneyLayout extends GridView {private int [] moneyList ={}; // data source private LayoutInflater mInflater; private MyAdapter adapter; // adapter int defaultChoose = 0; // by default, the selected item public ChooseMoneyLayout (Context context, AttributeSet attrs) {super (context, attrs); setData () ;}public void setData () {mInflater = LayoutInflater. from (getContext (); // configure the adapter = new MyAdapter (); setAdapter (Adapter);}/*** sets the default project, * @ param defaultChoose */public void setdefadefapositon (int defaultChoose) {this. defaultChoose = defaultChoose; adapter. notifyDataSetChanged ();}/*** set the data source * @ param moneyData */public void setMoneyData (int [] moneyData) {this. moneyList = moneyData;} class MyAdapter extends BaseAdapter {private CheckBox checkBox; @ Override public int getCount () {return moneyList. le Ngth ;}@ 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.mo NeyPayCb = (CheckBox) convertView. findViewById (R. id. money_pay_cb); convertView. setTag (holder);} else {holder = (MyViewHolder) convertView. getTag ();} holder. moneyPayCb. setText (getItem (position) + "Yuan"); holder. moneyPayCb. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked ){ // Set the selected text color buttonView. setTextColor (getResources (). getColor (R. color. light_color_blue); // cancel the previous selection if (checkBox! = Null) {checkBox. setChecked (false);} checkBox = (CheckBox) buttonView;} else {checkBox = null; // you can disable the selected text color, buttonView. setTextColor (getResources (). getColor (R. color. darkgray);} // callback listener. chooseMoney (position, isChecked, (Integer) getItem (position) ;}}); if (position = defaultChoose) {defachchoose =-1; holder. moneyPayCb. setChecked (true); checkBox = holder. moneyPayCb;} return convertView;} private class MyViewHolder {private CheckBox moneyPayCb ;}} /*** solve nested display problems * @ 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) {this. listener = listener ;} public interface onChoseMoneyListener {/*** select the amount and return ** @ param position the position of the gridView * @ param isCheck whether to select * @ param moneyNum amount of money */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"> <! -- Select the image --> <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's 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" />

Code in the activity:

Private ChooseMoneyLayout moneyChoseMoney; private int money; // the currently selected amount private void initData () {// obtain the control moneyChoseMoney = (ChooseMoneyLayout) findViewById (R. id. money_chose_money); // sets the data source moneyChoseMoney. setMoneyData (new int [] {30, 50,100,200,300,500,100 0}); // you can specify the default value of moneyChoseMoney. setdefapopositon (3); // select the amount to listen to 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 ;}}});}

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.