下面列出了怎么用android.widget.Checkable的API类实例代码及写法,或者点击链接到github查看源代码。
public void onChildViewAdded(View parent, View child) {
if (parent == ToggleButtonGroup.this && child instanceof Checkable) {
if (child.getId() == View.NO_ID) {
child.setId(generateIdForView(child));
}
if (child instanceof ToggleButton) {
setStateTracker((ToggleButton) child);
} else if (child instanceof CompoundButton) {
setStateTracker((CompoundButton) child);
}
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
/**
* Perform a quick, in-place update of the checked or activated state on all visible item views. This should only be called when
* a valid choice mode is active.
*/
private void updateOnScreenCheckedViews() {
final int firstPos = mFirstPosition;
final int count = getChildCount();
final boolean useActivated = android.os.Build.VERSION.SDK_INT >= 11;
for ( int i = 0; i < count; i++ ) {
final View child = getChildAt( i );
final int position = firstPos + i;
if ( child instanceof Checkable ) {
( (Checkable) child ).setChecked( mCheckStates.get( position ) );
} else if ( useActivated ) {
child.setActivated( mCheckStates.get( position ) );
}
}
}
private void updateOnScreenCheckedViews() {
boolean useActivated;
int firstPos = this.mFirstPosition;
int count = getChildCount();
if (VERSION.SDK_INT >= 11) {
useActivated = true;
} else {
useActivated = false;
}
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int position = firstPos + i;
if (child instanceof Checkable) {
((Checkable) child).setChecked(((Boolean) this.mCheckStates.get(position, Boolean.valueOf(false))).booleanValue());
} else if (useActivated) {
child.setActivated(((Boolean) this.mCheckStates.get(position, Boolean.valueOf(false))).booleanValue());
}
}
}
@Override
public void addView(View child, int index, LayoutParams params) {
if (child instanceof Checkable) {
final Checkable checkable = (Checkable) child;
if (checkable.isChecked()) {
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
if (child.getId() == View.NO_ID) {
child.setId(generateIdForView(child));
}
setCheckedId(child.getId());
}
if (child instanceof MarkerButton) {
((MarkerButton) child).setRadioStyle(true);
}
}
super.addView(child, index, params);
}
public void setInnerView(View innerView) {
if (mInnerView != null) {
removeView(innerView);
}
mInnerView = innerView;
addView(innerView);
if (mInnerView instanceof Checkable) {
((Checkable)mInnerView).setChecked(mChecked);
}
if (mSwipeState == STATE_PRESSED) {
cancelPressed();
mSwipeState = STATE_READY;
} else if (mSwipeState != STATE_READY) {
cancelSwipe();
}
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
View res = getView();
if (res == null) return;
boolean enabled = ((Checkable) res.findViewById(R.id.notifications_enabled)).isChecked();
P.set("notifications", enabled ? "true" : "false");
res.findViewById(R.id.notify_all).setEnabled(enabled);
res.findViewById(R.id.notify_direct).setEnabled(enabled);
res.findViewById(R.id.notify_direct_and_created).setEnabled(enabled);
res.findViewById(R.id.hour).setEnabled(enabled);
res.findViewById(R.id.half_day).setEnabled(enabled);
res.findViewById(R.id.day).setEnabled(enabled);
if (enabled && b) {
notify_which_listener.onCheckedChanged(null, true);
notify_duration_listener.onCheckedChanged(null, true);
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof Checkable) {
final Checkable button = (Checkable) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(child.getId());
}
}
super.addView(child, index, params);
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
if (v instanceof Checkable) {
if (((Checkable) v).isChecked()) {
setActiveUser(position);
} else {
((Checkable) v).setChecked(true);
}
} else {
showUserProfile(position);
}
}
@Override
public WrapperView getView(int position, View convertView, ViewGroup parent) {
WrapperView wv = (convertView == null) ? new WrapperView(mContext) : (WrapperView) convertView;
View item = mDelegate.getView(position, wv.mItem, parent);
View header = null;
if (previousPositionHasSameHeader(position)) {
recycleHeaderIfExists(wv);
} else {
header = configureHeader(wv, position);
}
if((item instanceof Checkable) && !(wv instanceof CheckableWrapperView)) {
// Need to create Checkable subclass of WrapperView for ListView to work correctly
wv = new CheckableWrapperView(mContext);
} else if(!(item instanceof Checkable) && (wv instanceof CheckableWrapperView)) {
wv = new WrapperView(mContext);
}
wv.update(item, header, mDivider, mDividerHeight);
return wv;
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
if (checkboxView != null && checkboxView instanceof Checkable) {
((Checkable) checkboxView).setChecked(mChecked);
}
syncSummaryView(view);
}
private void setUpChildCheckStatus(View view, boolean checked) {
view.setSelected(checked);
if (view instanceof Checkable && !(view instanceof BaseRadioLayout)){
((Checkable) view).setChecked(checked);
view.setFocusable(false);
view.setClickable(false);
view.setFocusableInTouchMode(false);
}
if (view instanceof ViewGroup){
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
setUpChildCheckStatus(group.getChildAt(i), checked);
}
}
}
/**
* Determines if the supplied {@link View} is one which would produce speech if it were to gain
* accessibility focus.
*
* <p>NOTE: This method also evaluates the subtree of the {@code view} for children that should be
* included in {@code view}'s spoken description.
*
* @param view The {@link View} to evaluate
* @return {@code true} if a spoken description for {@code view} was determined, {@code false}
* otherwise.
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN) // Calls hasNonActionableSpeakingChildren
private static boolean isSpeakingView(View view) {
if (hasText(view)) {
return true;
} else if (view instanceof Checkable) {
// Special case for checkable items, which screen readers may describe without text
return true;
} else if (hasNonActionableSpeakingChildren(view)) {
return true;
}
return false;
}
/**
* Sets the checked status of a checkable.
*
* @param viewId The view id.
* @param checked The checked status;
* @return The BaseViewHolder for chaining.
*/
public BaseViewHolder setChecked(@IdRes int viewId, boolean checked) {
View view = getView(viewId);
// View unable cast to Checkable
if (view instanceof Checkable) {
((Checkable) view).setChecked(checked);
}
return this;
}
@Override
public void onClick(View view) {
if (view instanceof Checkable) {
int position = (int) view.getTag();
mFilterKeywordsManager.toggleKeyword(position);
}
}
public View getView(int position, View viewWithoutSelection) {
if (viewWithoutSelection instanceof Checkable) {
long handle = positionToSelectionHandle(position);
boolean selected = isChecked(handle);
ignoreCheckedListener = true;
((Checkable) viewWithoutSelection).setChecked(selected);
ignoreCheckedListener = false;
}
if (itemIncludesCheckBox(viewWithoutSelection)) {
initItemCheckbox(position, (ViewGroup) viewWithoutSelection);
}
return viewWithoutSelection;
}
@Override
public boolean isChecked()
{
View child = getChildAt(0);
if (child instanceof Checkable)
return ((Checkable) child).isChecked();
else
return false;
}
@Override
public void setChecked(boolean checked)
{
View child = getChildAt(0);
if (child instanceof Checkable)
((Checkable) child).setChecked(checked);
}
@Override
public void toggle()
{
View child = getChildAt(0);
if (child instanceof Checkable)
((Checkable) child).toggle();
}
@Override
public void setChecked(boolean checked) {
if (checked == this.mChecked) {
return;
}
this.mChecked = checked;
for (Checkable checkable : mCheckablesSet) {
checkable.setChecked(checked);
}
refreshDrawableState();
}
private void syncCheckboxView(View view) {
if (view instanceof CompoundButton) {
((CompoundButton) view).setOnCheckedChangeListener(null);
}
if (view instanceof Checkable) {
((Checkable) view).setChecked(mChecked);
}
if (view instanceof CompoundButton) {
((CompoundButton) view).setOnCheckedChangeListener(mListener);
}
}
public boolean isChecked() {
if (mContent instanceof Checkable) {
return ((Checkable) mContent).isChecked();
}
throw new UnsupportedOperationException(
"This Litho component can't be checked, we can't determine its check state.");
}
@Override
public void setChecked(boolean checked) {
View child = getChildAt(0);
if (child instanceof Checkable) {
((Checkable) child).setChecked(checked);
}
}
protected Checkable getCheckableView(final View view) {
if (view instanceof Checkable) {
return (Checkable) view;
} else if (view instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
final View v = group.getChildAt(i);
if (v instanceof Checkable) {
return (Checkable) v;
}
}
}
return null;
}
/**
* Set Value of the Widget
*
* @param v
* Widget
* @param wd
* WidgetDescription instance
*/
protected void setValue(View v, WidgetDescription wd) {
// Checkboxes, radio buttons and toggle buttons -> the value is the
// checked state (true or false)
if (v instanceof Checkable) {
wd.setValue(String.valueOf(((Checkable) v).isChecked()));
}
// Textview, Editview et al. -> the value is the displayed text
if (v instanceof TextView) {
wd.setValue(((TextView) v).getText().toString());
// wd.setValue("");
return;
}
// Progress bars, seek bars and rating bars -> the value is the current
// progress
if (v instanceof ProgressBar) {
//wd.setValue(String.valueOf(((ProgressBar) v).getProgress()));
wd.setValue("");
}
if (v instanceof ImageView) {
ImageView imgView = (ImageView) v;
// TODO:
}
}
/**
* 设置View是否可选
*/
public RcvHolder setChecked(int viewId, boolean checked)
{
Checkable view = (Checkable) findView(viewId);
view.setChecked(checked);
return this;
}
public void setChecked(boolean isChecked)
{
this.isChecked = isChecked;
for (Checkable c : checkableViews)
{
c.setChecked(isChecked);
}
}
/**
* 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));
}
}
}
@TargetApi(HONEYCOMB)
public void setViewChecked(View view, boolean checked) {
if (view instanceof Checkable) {
((Checkable) view).setChecked(checked);
} else if (Build.VERSION.SDK_INT >= HONEYCOMB) {
view.setActivated(checked);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// apply checked state
if (convertView instanceof Checkable){
((Checkable) convertView).setChecked(isItemChecked(position));
if (mNoAnimations){ // suppresses the check animation when filtering
convertView.jumpDrawablesToCurrentState();
}
}
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DragSortItemView v;
View child;
// Log.d("mobeta",
// "getView: position="+position+" convertView="+convertView);
if (convertView != null) {
v = (DragSortItemView) convertView;
View oldChild = v.getChildAt(0);
child = mAdapter.getView(position, oldChild, DragSortListView.this);
if (child != oldChild) {
// shouldn't get here if user is reusing convertViews
// properly
if (oldChild != null) {
v.removeViewAt(0);
}
v.addView(child);
}
} else {
child = mAdapter.getView(position, null, DragSortListView.this);
if (child instanceof Checkable) {
v = new DragSortItemViewCheckable(getContext());
} else {
v = new DragSortItemView(getContext());
}
v.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
v.addView(child);
}
// Set the correct item height given drag state; passed
// View needs to be measured if measurement is required.
adjustItem(position + getHeaderViewsCount(), v, true);
return v;
}