超方便的替换RadioGroup的工具类
2019-12-03 本文已影响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;
}
}