| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package eVVM.apk.helper;
- import android.support.annotation.IdRes;
- import android.view.View;
- import android.widget.CompoundButton;
- import android.widget.RadioButton;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * desc : 多个 CompoundButton 选中处理辅助类(用于代替 RadioGroup)
- */
- public final class RadioButtonGroupHelper implements CompoundButton.OnCheckedChangeListener {
- private List<RadioButton> mViewSet;//RadioButton集合
- private OnCheckedChangeListener mListener;//多个RadioButton监听对象
- public RadioButtonGroupHelper(RadioButton... groups) {
- mViewSet = new ArrayList<>(groups.length - 1);
- for (RadioButton view : groups) {
- // 如果这个RadioButton没有设置id的话
- if (view.getId() == View.NO_ID) {
- throw new IllegalArgumentException("The resource id must be set for the RadioButton");
- }
- view.setOnCheckedChangeListener(this);
- mViewSet.add(view);
- }
- }
- public RadioButtonGroupHelper(View rootView, @IdRes int... ids) {
- mViewSet = new ArrayList<>(ids.length - 1);
- for (int id : ids) {
- RadioButton view = rootView.findViewById(id);
- view.setOnCheckedChangeListener(this);
- mViewSet.add(view);
- }
- }
- private boolean mTag; // 监听标记,避免重复回调
- /**
- * {@link CompoundButton.OnCheckedChangeListener}
- */
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked && !mTag) {
- mTag = true;
- for (CompoundButton view : mViewSet) {
- if (view != buttonView && view.isChecked()) {
- // 这个 API 会触发监听事件
- view.setChecked(false);
- }
- }
- if (mListener != null) {
- mListener.onCheckedChanged((RadioButton) buttonView, buttonView.getId());
- }
- mTag = false;
- }
- }
- /**
- * 移除监听,避免内存泄露
- */
- public void removeViews() {
- if (mViewSet == null) return;
- for (CompoundButton view : mViewSet) {
- view.setOnCheckedChangeListener(null);
- }
- mViewSet.clear();
- mViewSet = null;
- }
- /**
- * 取消选中
- */
- public void clearCheck() {
- for (CompoundButton view : mViewSet) {
- if (view.isChecked()) {
- view.setChecked(false);
- }
- }
- }
- /**
- * 设置多个RadioButton的监听
- */
- public void setOnCheckedChangeListener(OnCheckedChangeListener l) {
- mListener = l;
- }
- /**
- * 多个CompoundButton选中监听
- */
- public interface OnCheckedChangeListener {
- /**
- * 被选中的CompoundButton对象
- *
- * @param radioButton 选中的RadioButton
- * @param checkedId 选中的资源id
- */
- void onCheckedChanged(RadioButton radioButton, @IdRes int checkedId);
- }
- }
|