自訂View之GridView單選 金額選擇Layout-ChooseMoneyLayout,gridview單選

來源:互聯網
上載者:User

自訂View之GridView單選 金額選擇Layout-ChooseMoneyLayout,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;                }            }        });    }

  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.