下面列出了android.view.ViewParent#getParent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Scrolls to the element with the given label (<a name...) and focuses it
* (if focusable).
*/
public void gotoLabel(String label) {
View v = label == null ? this : labels.get(label);
if (v != null) {
if (v.isFocusable()) {
v.requestFocus();
}
int y = v.getTop();
ViewParent parent = v.getParent();
while (parent instanceof View) {
if (parent instanceof ScrollView) {
((ScrollView) parent).smoothScrollTo(0, y);
break;
}
y += ((View) parent).getTop();
parent = parent.getParent();
}
}
}
void identifySwipeRefreshParents() {
boolean cycle = true;
ViewParent parent = getParent();
if(parent != null) {
while(cycle) {
if(parent instanceof SwipeRefreshLayout) {
swipeRefreshLayout = (SwipeRefreshLayout)parent;
cycle = false;
} else {
if(parent.getParent() == null) {
cycle = false;
} else {
parent = parent.getParent();
}
}
}
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i(TAG, "bottomProgress onStopTrackingTouch [" + this.hashCode() + "] ");
startProgressTimer();
ViewParent vpup = getParent();
while (vpup != null) {
vpup.requestDisallowInterceptTouchEvent(false);
vpup = vpup.getParent();
}
if (state != STATE_PLAYING &&
state != STATE_PAUSE) return;
long time = seekBar.getProgress() * getDuration() / 100;
seekToManulPosition = seekBar.getProgress();
mediaInterface.seekTo(time);
Log.i(TAG, "seekTo " + time + " [" + this.hashCode() + "] ");
}
/**
* 获取 android.R.id.content View
* @param view {@link View}
* @param <T> 泛型
* @return {@link View}
*/
public static <T extends View> T getContentView(final View view) {
if (view != null) {
try {
ViewParent parent = view.getParent();
while (parent != null && parent instanceof View) {
View root = (View) parent;
if (root.getId() == android.R.id.content) {
return (T) root;
}
parent = parent.getParent();
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getContentView");
}
}
return null;
}
/**
* Sets up the action bar for an {@link PreferenceScreen}.
* @param preferenceScreen the preference screen on which to set the action bar.
*/
private static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
View.OnClickListener dismissDialogClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
};
ViewParent homeBtnContainer = homeBtn.getParent();
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) containerParent.setOnClickListener(dismissDialogClickListener);
else ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
} else homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
private void performAdapterViewItemClick(MotionEvent e) {
ViewParent t = getParent();
Log.d(TAG, "performAdapterViewItemClick()");
while (t != null) {
if (t instanceof AdapterView) {
@SuppressWarnings("rawtypes")
AdapterView view = (AdapterView) t;
int p = view.getPositionForView(ZSwipeItem.this);
if (p != AdapterView.INVALID_POSITION
&& view.performItemClick(
view.getChildAt(p
- view.getFirstVisiblePosition()), p,
view.getAdapter().getItemId(p)))
return;
} else {
if (t instanceof View && ((View) t).performClick())
return;
}
t = t.getParent();
}
}
private boolean shouldInheritContext(ViewParent parent) {
if (parent == null) {
// The initial parent is null so just return false
return false;
}
final View windowDecor = mWindow.getDecorView();
while (true) {
if (parent == null) {
// Bingo. We've hit a view which has a null parent before being terminated from
// the loop. This is (most probably) because it's the root view in an inflation
// call, therefore we should inherit. This works as the inflated layout is only
// added to the hierarchy at the end of the inflate() call.
return true;
} else if (parent == windowDecor || !(parent instanceof View)
|| ViewCompat.isAttachedToWindow((View) parent)) {
// We have either hit the window's decor view, a parent which isn't a View
// (i.e. ViewRootImpl), or an attached view, so we know that the original parent
// is currently added to the view hierarchy. This means that it has not be
// inflated in the current inflate() call and we should not inherit the context.
return false;
}
parent = parent.getParent();
}
}
@Override
public void requestLayout() {
// Don't request a layout if it will be blocked by any parent. Requesting a layout that is
// then ignored by an ancestor means that this host will remain in a state where it thinks that
// it has requested layout, and will therefore ignore future layout requests. This will lead to
// problems if a child (e.g. a ViewPager) requests a layout later on, since the request will be
// wrongly ignored by this host.
ViewParent parent = this;
while (parent instanceof ComponentHost) {
final ComponentHost host = (ComponentHost) parent;
if (!host.shouldRequestLayout()) {
return;
}
parent = parent.getParent();
}
super.requestLayout();
}
public static boolean isInScrollingContainer(ViewParent p) {
while (p != null && p instanceof ViewGroup) {
if (((ViewGroup) p).shouldDelayChildPressedState()) {
return true;
}
p = p.getParent();
}
return false;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i(TAG, "bottomProgress onStartTrackingTouch [" + this.hashCode() + "] ");
cancelProgressTimer();
ViewParent vpdown = getParent();
while (vpdown != null) {
vpdown.requestDisallowInterceptTouchEvent(true);
vpdown = vpdown.getParent();
}
}
private boolean isInScrollingContainer() {
ViewParent p = getParent();
while (p != null && p instanceof ViewGroup) {
if (((ViewGroup) p).shouldDelayChildPressedState()) {
return true;
}
p = p.getParent();
}
return false;
}
public static boolean isInScrollingContainer(ViewParent p) {
while (p != null && p instanceof ViewGroup) {
if (((ViewGroup) p).shouldDelayChildPressedState()) {
return true;
}
p = p.getParent();
}
return false;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ViewParent parent = this.getParent();
while (parent != null) {
if (parent instanceof ScrollDownLayout) {
((ScrollDownLayout) parent).setAssociatedScrollView(this);
break;
}
parent = parent.getParent();
}
}
@Nullable
private TextInputLayout getTextInputLayout() {
ViewParent parent = getParent();
while (parent instanceof View) {
if (parent instanceof TextInputLayout) {
return (TextInputLayout) parent;
}
parent = parent.getParent();
}
return null;
}
/**
* If this returns true, we can't start dragging the Slider immediately when we receive a {@link
* MotionEvent#ACTION_DOWN}. Instead, we must wait for a {@link MotionEvent#ACTION_MOVE}. Copied
* from hidden method of {@link View} isInScrollingContainer.
*
* @return true if any of this View's parents is a scrolling View.
*/
private boolean isInScrollingContainer() {
ViewParent p = getParent();
while (p instanceof ViewGroup) {
if (((ViewGroup) p).shouldDelayChildPressedState()) {
return true;
}
p = p.getParent();
}
return false;
}
protected boolean isViewDescendant(View v) {
ViewParent parent = v.getParent();
while (parent != null) {
if (parent == this) {
return true;
}
parent = parent.getParent();
}
return false;
}
private AdapterView getAdaptedView(View view) {
ViewParent parent = view.getParent();
while (parent != null) {
if (parent instanceof AdapterView) {
return (AdapterView) parent;
}
parent = parent.getParent();
}
return null;
}
/**
* 如果是ListView或RecyclerView的ItemView的话就返回index
*
* @param view
* @return
*/
private static void setIndexIfNeed(View view, List<String> msgList) {
if (view == null) {
return;
}
int index = 0;
ViewParent parent = view.getParent();
while (true) {
if (index > MAX_HIERARCHY_COUNT) {
return;
}
if (null == parent || !(parent instanceof View)) {
return;
}
if (parent instanceof ListView) {
msgList.add(getItemInfo((View) parent, view));
return;
}
if (parent instanceof RecyclerView) {
msgList.add(getItemInfo((View) parent, view));
return;
}
parent = parent.getParent();
++index;
}
}
/**
* Returns whether a given {@link View} will be focusable by Google's TalkBack screen reader.
*
* @param view The {@link View} to evaluate.
* @return {@code boolean} if the view will be ignored by TalkBack.
*/
public static boolean isTalkbackFocusable(View view) {
if (view == null) {
return false;
}
final int important = ViewCompat.getImportantForAccessibility(view);
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO
|| important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return false;
}
// Go all the way up the tree to make sure no parent has hidden its descendants
ViewParent parent = view.getParent();
while (parent instanceof View) {
if (ViewCompat.getImportantForAccessibility((View) parent)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return false;
}
parent = parent.getParent();
}
// Trying to evaluate the focusability of certain element types (mainly list views) can cause
// problems when trying to determine the offset of the elements Rect relative to its parent in
// ViewGroup.offsetRectBetweenParentAndChild. If this happens, simply return false, as this view
// will not be focusable.
AccessibilityNodeInfoCompat node;
try {
node = createNodeInfoFromView(view);
} catch (IllegalArgumentException e) {
return false;
}
if (node == null) {
return false;
}
// Non-leaf nodes identical in size to their Window should not be focusable.
if (areBoundsIdenticalToWindow(node, view) && node.getChildCount() > 0) {
return false;
}
try {
if (!node.isVisibleToUser()) {
return false;
}
if (isAccessibilityFocusable(node, view)) {
if (!hasVisibleChildren(view)) {
// Leaves that are accessibility focusable are never ignored, even if they don't have a
// speakable description
return true;
} else if (isSpeakingNode(node, view)) {
// Node is focusable and has something to speak
return true;
}
// Node is focusable and has nothing to speak
return false;
}
// if view is not accessibility focusable, it needs to have text and no focusable ancestors.
if (!hasText(node)) {
return false;
}
if (!hasFocusableAncestor(node, view)) {
return true;
}
return false;
} finally {
node.recycle();
}
}
/** Sets up the action bar for an {@link PreferenceScreen} */
@SuppressLint("NewApi")
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}