package com.jbirdvegas.mgerrit.objects; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.Checkable; import android.widget.RelativeLayout; import java.util.ArrayList; import java.util.List; /* * Copyright (C) 2013 Android Open Kang Project (AOKP) * Author: Evan Conway (P4R4N01D), 2013 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * RelativeLayout that delegates checked events to its children * * Source: https://github.com/marvinlabs/android-tutorials-custom-selectable-listview */ public class CheckableView extends RelativeLayout implements Checkable { private boolean mIsChecked = false; private final List<Checkable> checkableViews; public CheckableView(Context context, AttributeSet attrs) { super(context, attrs); this.mIsChecked = false; this.checkableViews = new ArrayList<>(5); } @Override public void setChecked(boolean isChecked) { this.mIsChecked = isChecked; for (Checkable c : checkableViews) { c.setChecked(isChecked); } } @Override public boolean isChecked() { return mIsChecked; } @Override public void toggle() { this.mIsChecked = !this.mIsChecked; for (Checkable c : checkableViews) { c.toggle(); } } @Override public boolean performClick() { toggle(); return super.performClick(); } @Override protected void onFinishInflate() { super.onFinishInflate(); final int childCount = this.getChildCount(); for (int i = 0; i < childCount; ++i) { findCheckableChildren(this.getChildAt(i)); } } /** * Add to our checkable list all the children of the view that implement the interface Checkable */ private void findCheckableChildren(View v) { if (v instanceof Checkable) { this.checkableViews.add((Checkable) v); } if (v instanceof ViewGroup) { final ViewGroup vg = (ViewGroup) v; final int childCount = vg.getChildCount(); for (int i = 0; i < childCount; ++i) { findCheckableChildren(vg.getChildAt(i)); } } } }