RadioGroup的简单封装

来源:互联网 发布:淘宝手机尺寸是多少 编辑:程序博客网 时间:2024/06/05 20:29

RadioGroup的简单封装 如代码:

/** * @outher 孙磊磊 * create at 2015/12/29 10:44 * description 单行多列选择器 */public class SelectRadioGroup extends RadioGroup implements CompoundButton.OnCheckedChangeListener {    private ChecdChangeListener mListener;    private int mSelectPosition;    private int mItenWidth;    public SelectRadioGroup(Context context) {        this(context, null);    }    public SelectRadioGroup(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    private void initView() {        WindowManager wm = (WindowManager) getContext()                .getSystemService(Context.WINDOW_SERVICE);        mItenWidth = (wm.getDefaultDisplay().getWidth() - 2 * DensityUtils.dp2px(getContext(), 5)) / 3;        setGravity(Gravity.CENTER_VERTICAL);        setOrientation(LinearLayout.HORIZONTAL);        setPadding(getPx(5), getPx(5), getPx(5), getPx(5));        setItem("item1", "item2", "item3");    }    /**     * @outher 孙磊磊     * create at 2015/12/29 14:49     * description 提供数据源,设置item     */    public void setItem(String... strings) {        removeAllViews();        for (int i = 0; i < strings.length; i++) {            addView(getItem(strings[i]));        }        setSelectItem(mSelectPosition);    }    /**     * @outher 孙磊磊     * create at 2015/12/29 14:48     * description 初始化item,设置每个item宽度为屏幕三分之一左右     */    private RadioButton getItem(String str) {        RadioButton radioButton = new RadioButton(getContext());        LayoutParams lp = new RadioGroup.LayoutParams(mItenWidth, LayoutParams.MATCH_PARENT);        radioButton.setLayoutParams(lp);        radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));        radioButton.setGravity(Gravity.CENTER);        radioButton.setTextSize(16);        radioButton.setPadding(getPx(5), getPx(5), getPx(5), getPx(5));        radioButton.setTextColor(getContext().getResources().getColor(R.color.white));        radioButton.setOnCheckedChangeListener(this);        radioButton.setText(str);        return radioButton;    }    private int getPx(int dp) {        return DensityUtils.dp2px(getContext(), dp);    }    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        if (isChecked) {            for (int i = 0; i < getChildCount(); i++) {                RadioButton rb = (RadioButton) getChildAt(i);                if (buttonView == rb) {                    mSelectPosition = i;                    if (mListener != null) {                        mListener.seletItem(i);                    }                    rb.setTextColor(getContext().getResources().getColor(R.color.white));                    rb.setBackgroundResource(R.drawable.round_orange);                } else {                    rb.setTextColor(getContext().getResources().getColor(R.color.grey_color));                    rb.setBackgroundColor(getResources().getColor(R.color.bg_def));                }            }        }    }    public interface ChecdChangeListener {        public void seletItem(int position);    }    public void setListener(ChecdChangeListener listener) {        this.mListener = listener;    }    /**     * @outher 孙磊磊     * create at 2015/12/29 14:47     * description 设置指定的item     */    public void setSelectItem(int position) {        if (position >= 0 && position < getChildCount()) {            RadioButton rb = (RadioButton) getChildAt(position);//            rb.setSelected(true);            rb.performClick();        }    }    /**     * @outher 孙磊磊     * create at 2015/12/29 14:47     * description 返回当前选定的item下标     */    public int getSelectIndex() {        return mSelectPosition;    }}

使用时直接调用方法 

public void setItem(String... strings);
不限定item个数,不指定时默认为三个,可嵌套在HorizontalScrollView内部,用于多选项滑动
效果图如下:

0 0