Skip to content

Instantly share code, notes, and snippets.

@subinkrishna
Last active February 22, 2017 19:53
Show Gist options
  • Save subinkrishna/c2136f87c0cce8485f30cf2953fdcff5 to your computer and use it in GitHub Desktop.
Save subinkrishna/c2136f87c0cce8485f30cf2953fdcff5 to your computer and use it in GitHub Desktop.
[Views] A utility class to do batch operations on multiple views #tags: android, view, utility
/*
* Copyright (C) 2017 Subinkrishna Gopi
*
* 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.
*/
import android.support.annotation.NonNull;
import android.view.View;
/**
* Collection of utility methods for {@link android.view.View}
*
* @author Subinkrishna Gopi
*/
public class Views {
/* Private constructor */
private Views() {
throw new RuntimeException("Invalid operation!");
}
/**
* Action interface definition.
*
* @param <T>
*/
public interface Action<T> {
void call(@NonNull T t);
}
/* Show */
public static Action<View> SHOW = new Action<View>() {
@Override public void call(View v) {
v.setVisibility(View.VISIBLE);
}
};
/* Hide */
public static Action<View> HIDE = new Action<View>() {
@Override public void call(View v) {
v.setVisibility(View.GONE);
}
};
/* Enable */
public static Action<View> ENABLE = new Action<View>() {
@Override public void call(View v) {
v.setEnabled(true);
}
};
/* Disable */
public static Action<View> DISABLE = new Action<View>() {
@Override public void call(View v) {
v.setEnabled(false);
}
};
/**
* Applies the specified action to all the supplied views.
*
* @param action
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void apply(Action<T> action, T... views) {
if ((null != action) && (null != views) && (views.length > 0)) {
for (View v : views) {
if (null != v) action.call((T)v);
}
}
}
/**
* Sets the visibility of all the supplied views based on the flag.
*
* @param show {@code true} to make all views visible, {@code false} to hide
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void show(boolean show, T... views) {
apply(show ? SHOW : HIDE, views);
}
/**
* Sets the visibility of all the supplied views to {@link View#VISIBLE}
*
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void show(T... views) {
apply(SHOW, views);
}
/**
* Sets the visibility of all the supplied views to {@link View#GONE}
*
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void hide(T... views) {
apply(HIDE, views);
}
/**
* Enables all the supplied views based on the flag.
*
* @param show {@code true} to enable all views, {@code false} to disable
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void enable(boolean show, T... views) {
apply(show ? ENABLE : DISABLE, views);
}
/**
* Attaches the given {@link android.view.View.OnClickListener} to all the aupplied {@link View}
* instances.
*
* @param listener
* @param views
* @param <T>
*/
@SafeVarargs @SuppressWarnings("unused")
public static <T extends View> void onClickListener(final View.OnClickListener listener,
T... views) {
if ((null != views) && (views.length > 0)) {
apply(new Action<View>() {
@Override public void call(View v) {
v.setOnClickListener(listener);
}
}, views);
}
}
}
// Built-in actions
Views.show(v1, v2, v3);
Views.show(false, v1, v2, v3);
Views.hide(v1, v2, v3);
Views.enable(true, v1, v2, v3);
// Custom Actions
// 1.7
Views.apply(new Action<View> {
public void call(View v) {
v.setOnClickListener(listener);
},
v1, v2, v3
});
// 1.8
Views.apply(v -> v.setOnClickListener(listener), v1, v2, v3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment