Use group in ConstraintLayout to listen for click events on multiple views

前端 未结 6 1370
时光取名叫无心
时光取名叫无心 2020-12-24 11:48

Basically I\'d like to attach a single OnClickListener to multiple views inside a ConstraintLayout.

Before migrating to the ConstraintLayout the views where inside o

6条回答
  •  猫巷女王i
    2020-12-24 12:17

    For the poor Java people out there like me:

    public class MyConstraintLayoutGroup extends Group {
        public MyConstraintLayoutGroup(Context context) {
            super(context);
        }
    
        public MyConstraintLayoutGroup(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyConstraintLayoutGroup(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public void setOnClickListener(OnClickListener listener) {
            for (int id : getReferencedIds()) {
                getRootView().findViewById(id).setOnClickListener(listener);
            }
        }
    }
    

    I don't like that this is not propagating click states to all other children.

提交回复
热议问题