美文网首页
超方便的替换RadioGroup的工具类

超方便的替换RadioGroup的工具类

作者: 进击的鱼儿 | 来源:发表于2019-12-03 13:57 被阅读0次
    public class SingleChoiceAdapter {
        private ViewGroup parent;
        private OnItemClickListener onItemClickListener;
    
        public SingleChoiceAdapter(ViewGroup parent) {
            this.parent = parent;
            View firstChild = parent.getChildAt(0);
            this.parent.setTag(firstChild);
            for (int i = 0; i < parent.getChildCount(); i++) {
                View child = parent.getChildAt(i);
                child.setTag(i);
                child.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick((Integer) v.getTag(), v);
                        }
                        ((View) SingleChoiceAdapter.this.parent.getTag()).setSelected(false);
                        v.setSelected(true);
                        SingleChoiceAdapter.this.parent.setTag(v);
                    }
                });
            }
        }
    
        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }
    }
    

    相关文章

      网友评论

          本文标题:超方便的替换RadioGroup的工具类

          本文链接:https://www.haomeiwen.com/subject/vktcgctx.html