private void recomputeScrollPositionHorizontal(int width, int oldWidth, int margin, int oldMargin) { if (oldWidth > 0 && !mItems.isEmpty()) { if (!mScroller.isFinished()) { mScroller.setFinalX(getCurrentItem() * getClientWidth()); } else { final int widthWithMargin = width - getPaddingLeft() - getPaddingRight() + margin; final int oldWidthWithMargin = oldWidth - getPaddingLeft() - getPaddingRight() + oldMargin; final int xpos = getScrollX(); final float pageOffset = (float) xpos / oldWidthWithMargin; final int newOffsetPixels = (int) (pageOffset * widthWithMargin); scrollTo(newOffsetPixels, getScrollY()); } } else { final YViewPager.ItemInfo ii = infoForPosition(mCurItem); final float scrollOffset = ii != null ? Math.min(ii.offset, mLastOffset) : 0; final int scrollPos = (int) (scrollOffset * (width - getPaddingLeft() - getPaddingRight())); if (scrollPos != getScrollX()) { completeScrollHorizontal(false); scrollTo(scrollPos, getScrollY()); } } }
boolean pageRight() { if (mAdapter != null && mCurItem < (getAdapterCount() - 1)) { setCurrentItem(mCurItem + 1, true); return true; } return false; }
@Override public void addView(View child, int index, ViewGroup.LayoutParams params) { if (!checkLayoutParams(params)) { params = generateLayoutParams(params); } final YViewPager.LayoutParams lp = (YViewPager.LayoutParams) params; // Any views added via inflation should be classed as part of the decor lp.isDecor |= isDecorView(child); if (mInLayout) { if (lp != null && lp.isDecor) { throw new IllegalStateException("Cannot add pager decor view during layout"); } lp.needsMeasure = true; addViewInLayout(child, index, params); } else { ViewParent parent = child.getParent(); if (parent != null) { ((ViewGroup) parent).removeView(child); } super.addView(child, index, params); } if (USE_CACHE) { if (child.getVisibility() != GONE) { child.setDrawingCacheEnabled(mScrollingCacheEnabled); } else { child.setDrawingCacheEnabled(false); } } }
private void recomputeScrollPositionVertical(int height, int oldHeight, int margin, int oldMargin) { if (oldHeight > 0 && !mItems.isEmpty()) { if (!mScroller.isFinished()) { mScroller.setFinalY(getCurrentItem() * getClientHeight()); } else { final int heightWithMargin = height - getPaddingTop() - getPaddingBottom() + margin; final int oldHeightWithMargin = oldHeight - getPaddingTop() - getPaddingBottom() + oldMargin; final int ypos = getScrollY(); final float pageOffset = (float) ypos / oldHeightWithMargin; final int newOffsetPixels = (int) (pageOffset * heightWithMargin); scrollTo(getScrollX(), newOffsetPixels); } } else { final ItemInfo ii = infoForPosition(mCurItem); final float scrollOffset = ii != null ? Math.min(ii.offset, mLastOffset) : 0; final int scrollPos = (int) (scrollOffset * (height - getPaddingTop() - getPaddingBottom())); if (scrollPos != getScrollY()) { completeScrollVertical(false); scrollTo(getScrollX(), scrollPos); } } }
void populateVertical(int newCurrentItem) { ItemInfo oldCurInfo = null; if (mCurItem != newCurrentItem) { oldCurInfo = infoForPosition(mCurItem); mCurItem = newCurrentItem; sortChildDrawingOrder(); return; sortChildDrawingOrder(); return; if (getWindowToken() == null) { return; final int N = getAdapterCount(); final int endPos = Math.min(N - 1, mCurItem + pageLimit); if (N != mExpectedAdapterCount) { String resName; try { resName = getResources().getResourceName(getId()); } catch (Resources.NotFoundException e) { resName = Integer.toHexString(getId()); curItem = addNewItem(mCurItem, curIndex); int itemIndex = curIndex - 1; ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null; final int clientHeight = getClientHeight();
void populateHorizontal(int newCurrentItem) { ItemInfo oldCurInfo = null; if (mCurItem != newCurrentItem) { oldCurInfo = infoForPosition(mCurItem); mCurItem = newCurrentItem; sortChildDrawingOrder(); return; sortChildDrawingOrder(); return; if (getWindowToken() == null) { return; final int N = getAdapterCount(); final int endPos = Math.min(N - 1, mCurItem + pageLimit); if (N != mExpectedAdapterCount) { String resName; try { resName = getResources().getResourceName(getId()); } catch (Resources.NotFoundException e) { resName = Integer.toHexString(getId()); curItem = addNewItem(mCurItem, curIndex); final int clientWidth = getClientWidth(); 2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth;
resetTouch(); return false; if (!isVertical && dx != 0 && !isGutterDragHorizontal(mLastMotionX, dx) && canScrollHorizontal(this, false, (int) dx, (int) x, (int) y)) { mIsUnableToDrag = true; return false; } else if (isVertical && dy != 0 && isGutterDragVertical(mLastMotionY, dy) && canScrollVertical(this, false, (int) dy, (int) x, (int) y)) { mLastMotionX = x; mLastMotionY = y; requestParentDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop; mLastMotionY = y; setScrollingCacheEnabled(true); } else if (!isVertical && yDiff > mTouchSlop) { } else if (isVertical && yDiff > mTouchSlop && yDiff * 0.5f > xDiff) { mIsBeingDragged = true; requestParentDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); mLastMotionY = dy > 0 ? mInitialMotionY + mTouchSlop : mInitialMotionY - mTouchSlop; mLastMotionX = x; setScrollingCacheEnabled(true);
mYViewPager.setAdapter(new ImagePagerAdapter()); mYViewPager.setCurrentItem(0); mYViewPager.addOnPageChangeListener(new YViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (super.performAccessibilityAction(host, action, args)) { return true; } switch (action) { case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD: { if (canScrollHorizontally(1)) { setCurrentItem(mCurItem + 1); return true; } } return false; case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD: { if (canScrollHorizontally(-1)) { setCurrentItem(mCurItem - 1); return true; } } return false; } return false; }
initData(); mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager())); mViewPager.setPageMargin(10); mViewPager.addOnPageChangeListener(new YViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
boolean pageLeft() { if (mCurItem > 0) { setCurrentItem(mCurItem - 1, true); return true; } return false; }
@Override public void onPageSelected(int position) { setIndicator(position%mImageViews.size()); Log.d(TAG,"POSITION=====>"+mYViewPager.getCurrentItem()); }
switch (event.getKeyCode()) { case KeyEvent.KEYCODE_DPAD_LEFT: handled = arrowScroll(FOCUS_LEFT); break; case KeyEvent.KEYCODE_DPAD_RIGHT: handled = arrowScroll(FOCUS_RIGHT); break; case KeyEvent.KEYCODE_TAB: handled = arrowScroll(FOCUS_FORWARD); } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) { handled = arrowScroll(FOCUS_BACKWARD);
/** * Tests scrollability within child views of v given a delta of dx. * * @param v View to test for horizontal scrollability * @param checkV Whether the view v passed should itself be checked for scrollability (true), * or just its children (false). * @param dx Delta scrolled in pixels * @param x X coordinate of the active touch point * @param y Y coordinate of the active touch point * @return true if child views of v can be scrolled by delta of dx. */ protected boolean canScrollHorizontal(View v, boolean checkV, int dx, int x, int y) { if (v instanceof ViewGroup) { final ViewGroup group = (ViewGroup) v; final int scrollX = v.getScrollX(); final int scrollY = v.getScrollY(); final int count = group.getChildCount(); for (int i = count - 1; i >= 0; i--) { // TODO: Add versioned support here for transformed views. final View child = group.getChildAt(i); if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() && y + scrollY >= child.getTop() && y + scrollY < child.getBottom() && canScrollHorizontal(child, true, dx, x + scrollX - child.getLeft(), y + scrollY - child.getTop())) { return true; } } } return checkV && ViewCompat.canScrollHorizontally(v, -dx); }
@Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setClassName(YViewPager.class.getName()); info.setScrollable(canScroll()); if (canScrollHorizontally(1)) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD); } if (canScrollHorizontally(-1)) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD); } }