List of usage examples for android.view View getParent
public final ViewParent getParent()
From source file:com.minoon.stackviewpagersample.StackViewPager.java
public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) { currentFocused = null;//from w w w. j av a2s. co m } else if (currentFocused != null) { boolean isChild = false; for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { if (parent == this) { isChild = true; break; } } if (!isChild) { // This would cause the focus search down below to fail in fun ways. final StringBuilder sb = new StringBuilder(); sb.append(currentFocused.getClass().getSimpleName()); for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { sb.append(" => ").append(parent.getClass().getSimpleName()); } // Log.e(TAG, "arrowScroll tried to find focus based on non-child " + // "current focused view " + sb.toString()); currentFocused = null; } } boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_LEFT) { // If there is nothing to the left, or this is causing us to // jump to the right, then what we really want to do is page left. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft >= currLeft) { handled = pageLeft(); } else { handled = nextFocused.requestFocus(); } } else if (direction == View.FOCUS_RIGHT) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft <= currLeft) { handled = pageRight(); } else { handled = nextFocused.requestFocus(); } } } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageLeft(); } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageRight(); } if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } return handled; }
From source file:com.android.backups.StaggeredGridViewByJaumo.java
/** * Should be called with mPopulating set to true * /*from ww w. ja v a2s . c om*/ * @param fromPosition * Position to start filling from * @param overhang * the number of extra pixels to fill beyond the current top edge * @return the max overhang beyond the beginning of the view of any added * items at the top */ final int fillUp(int fromPosition, int overhang) { final int paddingLeft = getPaddingLeft(); mColWidth = getColWidth(fromPosition); final int gridTop = getPaddingTop(); final int fillTo = gridTop - overhang; int nextCol = getNextColumnUp(); int position = fromPosition; while (nextCol >= 0 && mItemTops[nextCol] > fillTo && position >= 0) { // make sure the nextCol is correct. check to see if has been mapped // otherwise stick to getNextColumnUp() if (!mColMappings.get(nextCol).contains(position)) { for (int i = 0; i < mColMappings.size(); i++) { if (mColMappings.get(i).contains(position)) { nextCol = i; break; } } } final View child = obtainView(position, null); if (child == null) continue; LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (child.getParent() != this) { if (mInLayout) { addViewInLayout(child, 0, lp); } else { addView(child, 0); } } final int itemMargin = getItemMargin(lp.position); final int colWidth = getColWidth(lp.position); final int span = Math.min(mColCount, lp.span); final int widthSize = colWidth * span + itemMargin * (span - 1); final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); LayoutRecord rec; if (span > 1) { rec = getNextRecordUp(position, span); nextCol = 0; } else { rec = mLayoutRecords.get(position); } boolean invalidateBefore = false; if (rec == null) { rec = new LayoutRecord(); mLayoutRecords.put(position, rec); rec.column = nextCol; rec.span = span; } else if (span != rec.span) { rec.span = span; rec.column = nextCol; invalidateBefore = true; } if (mHasStableIds) { final long id = mAdapter.getItemId(position); rec.id = id; lp.id = id; } lp.column = nextCol; final int heightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } else { heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(widthSpec, heightSpec); final int childHeight = child.getMeasuredHeight(); if (invalidateBefore || (childHeight != rec.height && rec.height > 0)) { invalidateLayoutRecordsBeforePosition(position); } rec.height = childHeight; final int startFrom; if (span > 1) { int lowest = mItemTops[nextCol]; final int colEnd = Math.min(mColCount, nextCol + lp.span); for (int i = nextCol; i < colEnd; i++) { final int top = mItemTops[i]; if (top < lowest) { lowest = top; } } startFrom = lowest; } else { startFrom = mItemTops[nextCol]; } int childBottom = startFrom; int childTop = childBottom - childHeight; final int childLeft = paddingLeft + itemMargin + nextCol * (colWidth + itemMargin); final int childRight = childLeft + child.getMeasuredWidth(); child.layout(childLeft, childTop, childRight, childBottom); final int colEnd = Math.min(mColCount, nextCol + lp.span); for (int i = nextCol; i < colEnd; i++) { mItemTops[i] = childTop - itemMargin; } nextCol = getNextColumnUp(); mFirstPosition = position--; } int highestView = getHeight(); for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child == null) { break; } final int top = child.getTop(); if (top < highestView) { highestView = top; } } return gridTop - highestView; }
From source file:au.com.glassechidna.velocityviewpager.VelocityViewPager.java
public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) { currentFocused = null;// ww w . ja v a 2 s. co m } else if (currentFocused != null) { boolean isChild = false; for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { if (parent == this) { isChild = true; break; } } if (!isChild) { // This would cause the focus search down below to fail in fun ways. final StringBuilder sb = new StringBuilder(); sb.append(currentFocused.getClass().getSimpleName()); for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { sb.append(" => ").append(parent.getClass().getSimpleName()); } Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view " + sb.toString()); currentFocused = null; } } boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_LEFT) { // If there is nothing to the left, or this is causing us to // jump to the right, then what we really want to do is page left. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft >= currLeft) { handled = pageLeft(); } else { handled = nextFocused.requestFocus(); } } else if (direction == View.FOCUS_RIGHT) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft <= currLeft) { handled = pageRight(); } else { handled = nextFocused.requestFocus(); } } } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageLeft(); } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageRight(); } if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } return handled; }
From source file:com.origamilabs.library.views.StaggeredGridView.java
/** Should be called with mPopulating set to true * //w w w . j ava2s. c o m * @param fromPosition * Position to start filling from * @param overhang * the number of extra pixels to fill beyond the current bottom * edge * @return the max overhang beyond the end of the view of any added items at * the bottom */ final int fillDown(int fromPosition, int overhang) { final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); // final int itemMargin = mItemMargin; final int colWidth = (getWidth() - paddingLeft - paddingRight) / mColCount; final int gridBottom = getHeight() - getPaddingBottom(); final int fillTo = gridBottom + overhang; int nextCol = getNextColumnDown(fromPosition); int position = fromPosition; while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) { final View child = obtainView(position, null); if (child == null) continue; LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp == null) { lp = this.generateDefaultLayoutParams(); child.setLayoutParams(lp); } if (child.getParent() != this) { if (mInLayout) { addViewInLayout(child, -1, lp); } else { addView(child); } } final int span = Math.min(mColCount, lp.span); final int widthSize = colWidth * span; final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); LayoutRecord rec; if (span > 1) { rec = getNextRecordDown(position, span); // nextCol = rec.column; } else { rec = mLayoutRecords.get(position); } boolean invalidateAfter = false; if (rec == null) { rec = new LayoutRecord(); mLayoutRecords.put(position, rec); rec.column = nextCol; rec.span = span; } else if (span != rec.span) { rec.span = span; rec.column = nextCol; invalidateAfter = true; } else { // nextCol = rec.column; } if (mHasStableIds) { final long id = mAdapter.getItemId(position); rec.id = id; lp.id = id; } lp.column = nextCol; final int heightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } else { heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(widthSpec, heightSpec); final int childHeight = child.getMeasuredHeight(); if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) { invalidateLayoutRecordsAfterPosition(position); } rec.height = childHeight; final int startFrom; if (span > 1) { int lowest = mItemBottoms[nextCol]; for (int i = nextCol + 1; i < nextCol + span; i++) { final int bottom = mItemBottoms[i]; if (bottom > lowest) { lowest = bottom; } } startFrom = lowest; } else { startFrom = mItemBottoms[nextCol]; } final int childTop = startFrom; final int childBottom = childTop + childHeight; final int childLeft = paddingLeft + nextCol * (colWidth); final int childRight = childLeft + child.getMeasuredWidth(); child.layout(childLeft, childTop, childRight, childBottom); // add the position to the mapping if (!mColMappings.get(nextCol).contains(position)) { // check to see if the mapping exists in other columns // this would happen if list has been updated for (ArrayList<Integer> list : mColMappings) { if (list.contains(position)) { list.remove((Integer) position); } } mColMappings.get(nextCol).add(position); } for (int i = nextCol; i < nextCol + span; i++) { mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol); } position++; nextCol = getNextColumnDown(position); } int lowestView = 0; for (int i = 0; i < mColCount; i++) { if (mItemBottoms[i] > lowestView) { lowestView = mItemBottoms[i]; } } return lowestView - gridBottom; }
From source file:com.android.backups.StaggeredGridViewByJaumo.java
/** * Should be called with mPopulating set to true * //from ww w . j av a 2 s . co m * @param position * @param fillTo * @return the max overhang beyond the end of the view of any added items at * the bottom */ final int fillDownTo(int position, int fillTo) { final int gridBottom = getHeight() - getPaddingBottom(); final int paddingLeft = getPaddingLeft(); int nextCol = getNextColumnDown(position); while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) { final View child = obtainView(position, null); if (child == null) continue; LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int colWidth = getColWidth(lp.position); if (child.getParent() != this) { if (mInLayout) { addViewInLayout(child, -1, lp); } else { addView(child); } } final int span = Math.min(mColCount, lp.span); final int widthSize = colWidth * span + getItemMargin(position) * (span - 1); final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); LayoutRecord rec; if (span > 1) { rec = getNextRecordDown(position, span); nextCol = 0; } else { rec = mLayoutRecords.get(position); } boolean invalidateAfter = false; if (rec == null) { rec = new LayoutRecord(); mLayoutRecords.put(position, rec); rec.column = nextCol; rec.span = span; } else if (span != rec.span) { rec.span = span; rec.column = nextCol; invalidateAfter = true; } if (mHasStableIds) { final long id = mAdapter.getItemId(position); rec.id = id; lp.id = id; } lp.column = nextCol; final int heightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } else { heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(widthSpec, heightSpec); final int childHeight = child.getMeasuredHeight(); if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) { invalidateLayoutRecordsAfterPosition(position); } rec.height = childHeight; final int startFrom; if (span > 1) { int lowest = mItemBottoms[nextCol]; // Only for span = maxCol for (int i = 0; i < mColCount; i++) { final int bottom = mItemBottoms[i]; if (bottom > lowest) { lowest = bottom; } } startFrom = lowest; } else { startFrom = mItemBottoms[nextCol]; } int itemMargin = getItemMargin(lp.position); final int childTop = startFrom + itemMargin; final int childBottom = childTop + childHeight; final int childLeft; if (span > 1) { childLeft = paddingLeft + itemMargin; } else { childLeft = paddingLeft + itemMargin + nextCol * (colWidth + itemMargin); } final int childRight = childLeft + child.getMeasuredWidth(); child.layout(childLeft, childTop, childRight, childBottom); rec.left = childLeft; rec.top = childTop; rec.right = childRight; rec.bottom = childBottom; rec.hasRecRecord = true; // add the position to the mapping if (!mColMappings.get(nextCol).contains(position)) { // check to see if the mapping exists in other columns // this would happen if list has been updated for (ArrayList<Integer> list : mColMappings) { if (list.contains(position)) { list.remove((Integer) position); } } mColMappings.get(nextCol).add(position); } final int colEnd = Math.min(mColCount, nextCol + lp.span); for (int i = nextCol; i < colEnd; i++) { mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol); } position++; nextCol = getNextColumnDown(position); } int lowestView = 0; for (int i = 0; i < mColCount; i++) { if (mItemBottoms[i] > lowestView) { lowestView = mItemBottoms[i]; } } return lowestView - gridBottom; }
From source file:com.av.remusic.widget.RoundViewPager.java
public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) { currentFocused = null;// ww w .j a v a 2s . c om } else if (currentFocused != null) { boolean isChild = false; for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { if (parent == this) { isChild = true; break; } } if (!isChild) { // This would cause the focus search down below to fail in fun ways. final StringBuilder sb = new StringBuilder(); sb.append(currentFocused.getClass().getSimpleName()); for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { sb.append(" => ").append(parent.getClass().getSimpleName()); } Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "currentPosition focused view " + sb.toString()); currentFocused = null; } } boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_LEFT) { // If there is nothing to the left, or this is causing us to // jump to the right, then what we really want to do is page left. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft >= currLeft) { handled = pageLeft(); } else { handled = nextFocused.requestFocus(); } } else if (direction == View.FOCUS_RIGHT) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft <= currLeft) { handled = pageRight(); } else { handled = nextFocused.requestFocus(); } } } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageLeft(); } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageRight(); } if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } return handled; }
From source file:com.origamilabs.library.views.StaggeredGridView.java
/** Should be called with mPopulating set to true * //from w w w . j ava 2s .c o m * @param fromPosition * Position to start filling from * @param overhang * the number of extra pixels to fill beyond the current top edge * @return the max overhang beyond the beginning of the view of any added * items at the top */ final int fillUp(int fromPosition, int overhang) { final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); // final int itemMargin = mItemMargin; final int colWidth = (getWidth() - paddingLeft - paddingRight) / mColCount; mColWidth = colWidth; final int gridTop = getPaddingTop(); final int fillTo = gridTop - overhang; int nextCol = getNextColumnUp(); int position = fromPosition; while (nextCol >= 0 && mItemTops[nextCol] > fillTo && position >= 0) { // make sure the nextCol is correct. check to see if has been mapped // otherwise stick to getNextColumnUp() if (!mColMappings.get(nextCol).contains((Integer) position)) { for (int i = 0; i < mColMappings.size(); i++) { if (mColMappings.get(i).contains((Integer) position)) { nextCol = i; break; } } } // displayMapping(); final View child = obtainView(position, null); if (child == null) continue; LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp == null) { lp = this.generateDefaultLayoutParams(); child.setLayoutParams(lp); } if (child.getParent() != this) { if (mInLayout) { addViewInLayout(child, 0, lp); } else { addView(child, 0); } } final int span = Math.min(mColCount, lp.span); final int widthSize = colWidth * span; final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); LayoutRecord rec; if (span > 1) { rec = getNextRecordUp(position, span); // nextCol = rec.column; } else { rec = mLayoutRecords.get(position); } boolean invalidateBefore = false; if (rec == null) { rec = new LayoutRecord(); mLayoutRecords.put(position, rec); rec.column = nextCol; rec.span = span; } else if (span != rec.span) { rec.span = span; rec.column = nextCol; invalidateBefore = true; } else { // nextCol = rec.column; } if (mHasStableIds) { final long id = mAdapter.getItemId(position); rec.id = id; lp.id = id; } lp.column = nextCol; final int heightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } else { heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(widthSpec, heightSpec); final int childHeight = child.getMeasuredHeight(); if (invalidateBefore || (childHeight != rec.height && rec.height > 0)) { invalidateLayoutRecordsBeforePosition(position); } rec.height = childHeight; // int itemTop = mItemTops[nextCol]; final int startFrom; if (span > 1) { int highest = mItemTops[nextCol]; for (int i = nextCol + 1; i < nextCol + span; i++) { final int top = mItemTops[i]; if (top < highest) { highest = top; } } startFrom = highest; } else { startFrom = mItemTops[nextCol]; } int childBottom = startFrom; int childTop = childBottom - childHeight; final int childLeft = paddingLeft + nextCol * (colWidth); final int childRight = childLeft + child.getMeasuredWidth(); // if(position == 0){ // if(this.getChildCount()>1 && this.mColCount>1){ // childTop = this.getChildAt(1).getTop(); // childBottom = childTop + childHeight; // } // } child.layout(childLeft, childTop, childRight, childBottom); for (int i = nextCol; i < nextCol + span; i++) { mItemTops[i] = childTop - rec.getMarginAbove(i - nextCol); } nextCol = getNextColumnUp(); mFirstPosition = position--; } int highestView = getHeight(); for (int i = 0; i < mColCount; i++) { final View child = getFirstChildAtColumn(i); if (child == null) { highestView = 0; break; } final int top = child.getTop(); if (top < highestView) { highestView = top; } } return gridTop - highestView; }
From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java
public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) { currentFocused = null;//from w ww .j a v a 2 s . c o m } else if (currentFocused != null) { boolean isChild = false; for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { if (parent == this) { isChild = true; break; } } if (!isChild) { // This would cause the focus search down below to fail in fun ways. final StringBuilder sb = new StringBuilder(); sb.append(currentFocused.getClass().getSimpleName()); for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { sb.append(" => ").append(parent.getClass().getSimpleName()); } Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view " + sb.toString()); currentFocused = null; } } boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_LEFT) { // If there is nothing to the left, or this is causing us to // jump to the right, then what we really want to do is page left. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft >= currLeft) { handled = pageLeft(); } else { handled = nextFocused.requestFocus(); } } else if (direction == View.FOCUS_RIGHT) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. final int nextStart = this.orientation == LinearLayout.HORIZONTAL ? getChildRectInPagerCoordinates(mTempRect, nextFocused).left : getChildRectInPagerCoordinates(mTempRect, nextFocused).top; final int currentStart = this.orientation == LinearLayout.HORIZONTAL ? getChildRectInPagerCoordinates(mTempRect, currentFocused).left : getChildRectInPagerCoordinates(mTempRect, currentFocused).top; if (currentFocused != null && nextStart <= currentStart) { handled = pageRight(); } else { handled = nextFocused.requestFocus(); } } } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageLeft(); } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageRight(); } if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } return handled; }
From source file:com.example.sky.test.view.ViewPager.java
/** * Handle scrolling in response to a left or right arrow click. * * @param direction The direction corresponding to the arrow key that was pressed. It should be * either {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. * @return Whether the scrolling was handled successfully. *//*from ww w . jav a2 s . c om*/ public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) { currentFocused = null; } else if (currentFocused != null) { boolean isChild = false; for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { if (parent == this) { isChild = true; break; } } if (!isChild) { // This would cause the focus search down below to fail in fun ways. final StringBuilder sb = new StringBuilder(); sb.append(currentFocused.getClass().getSimpleName()); for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent .getParent()) { sb.append(" => ").append(parent.getClass().getSimpleName()); } Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view " + sb.toString()); currentFocused = null; } } boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_LEFT) { // If there is nothing to the left, or this is causing us to // jump to the right, then what we really want to do is page left. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft >= currLeft) { handled = pageLeft(); } else { handled = nextFocused.requestFocus(); } } else if (direction == View.FOCUS_RIGHT) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left; final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left; if (currentFocused != null && nextLeft <= currLeft) { handled = pageRight(); } else { handled = nextFocused.requestFocus(); } } } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageLeft(); } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageRight(); } if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } return handled; }
From source file:ca.psiphon.ploggy.FragmentSelfStatusDetails.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.self_status_details, container, false); mFragmentComposeMessage = new FragmentComposeMessage(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.fragment_self_status_details_compose_message, mFragmentComposeMessage).commit(); mScrollView = (ScrollView) view.findViewById(R.id.self_status_details_scroll_view); mAvatarImage = (ImageView) view.findViewById(R.id.self_status_details_avatar_image); mNicknameText = (TextView) view.findViewById(R.id.self_status_details_nickname_text); mFingerprintText = (TextView) view.findViewById(R.id.self_status_details_fingerprint_text); mMessagesList = (ListView) view.findViewById(R.id.self_status_details_messages_list); mLocationLabel = (TextView) view.findViewById(R.id.self_status_details_location_label); mLocationStreetAddressLabel = (TextView) view .findViewById(R.id.self_status_details_location_street_address_label); mLocationStreetAddressText = (TextView) view .findViewById(R.id.self_status_details_location_street_address_text); mLocationCoordinatesLabel = (TextView) view .findViewById(R.id.self_status_details_location_coordinates_label); mLocationCoordinatesText = (TextView) view.findViewById(R.id.self_status_details_location_coordinates_text); mLocationPrecisionLabel = (TextView) view.findViewById(R.id.self_status_details_location_precision_label); mLocationPrecisionText = (TextView) view.findViewById(R.id.self_status_details_location_precision_text); mLocationTimestampLabel = (TextView) view.findViewById(R.id.self_status_details_location_timestamp_label); mLocationTimestampText = (TextView) view.findViewById(R.id.self_status_details_location_timestamp_text); // TODO: use header/footer of listview instead of hack embedding of listview in scrollview // from: http://stackoverflow.com/questions/4490821/scrollview-inside-scrollview/11554823#11554823 mScrollView.setOnTouchListener(new View.OnTouchListener() { @Override// w ww. j a v a2 s . com public boolean onTouch(View view, MotionEvent event) { mMessagesList.requestDisallowInterceptTouchEvent(false); return false; } }); mMessagesList.setOnTouchListener(new View.OnTouchListener() { private float downX, downY; @Override public boolean onTouch(View view, MotionEvent event) { // We want to capture vertical scrolling motions for use by // the messages list, but we don't want to capture horizontal // swiping that should be used to switch tabs. So we're going // to decide based on whether the move looks more X-ish or Y-ish. if (event.getAction() == MotionEvent.ACTION_DOWN) { downX = event.getX(); downY = event.getY(); // Make sure the parent is allowed to intercept. view.getParent().requestDisallowInterceptTouchEvent(false); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { float deltaX = downX - event.getX(); float deltaY = downY - event.getY(); if (Math.abs(deltaY) > Math.abs(deltaX)) { // Looks like a Y-ish scroll attempt. Disallow parent from intercepting. view.getParent().requestDisallowInterceptTouchEvent(true); } } return false; } }); try { mMessageAdapter = new MessageAdapter(getActivity(), MessageAdapter.Mode.SELF_MESSAGES); mMessagesList.setAdapter(mMessageAdapter); } catch (Utils.ApplicationError e) { Log.addEntry(LOG_TAG, "failed to load self messages"); } show(view); // Refresh the message list every 5 seconds. This updates "time ago" displays. // TODO: event driven redrawing? mRefreshUIExecutor = new Utils.FixedDelayExecutor(new Runnable() { @Override public void run() { show(); } }, 5000); Events.register(this); return view; }