{"id":191974,"date":"2013-08-30T14:39:03","date_gmt":"2013-08-30T10:39:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=191974"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=191974","title":{"rendered":"<span class=\"post_title\">ViewPager \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0414\u043e\u0431\u0440\u044b\u0439 \u0434\u0435\u043d\u044c,<\/p>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u043d\u0435\u0440\u043d\u0443\u044e \u0433\u0430\u043b\u0435\u0440\u0435\u0439\u043a\u0443 \u0441 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0435\u0439, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0431\u044b\u043b\u0430 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0438 \u0441\u0430\u043c\u043e\u0439 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0435\u0439 \u0432 ViewPager \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0431\u044b\u043b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u044b\u0441\u0442\u0440\u044b\u0439, \u0438 \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0441 1 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 5 \u0442\u043e \u043d\u0435 \u0443\u0432\u0438\u0434\u0438\u0448\u044c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 3-4 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 <br \/>  <a name=\"habracut\"><\/a><br \/>  \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0447\u0435\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f ViewPager<\/p>\n<pre><code class=\"java\">void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {         if (mAdapter == null || mAdapter.getCount() &lt;= 0) {             setScrollingCacheEnabled(false);             return;         }         if (!always && mCurItem == item && mItems.size() != 0) {             setScrollingCacheEnabled(false);             return;         }          if (item &lt; 0) {             item = 0;         } else if (item &gt;= mAdapter.getCount()) {             item = mAdapter.getCount() - 1;         }         final int pageLimit = mOffscreenPageLimit; \/\/ \u0418\u0437-\u0437\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043c\u044b \u043d\u0435 \u0432\u0438\u0434\u0438\u043c n-\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b          if (item &gt; (mCurItem + pageLimit) || item &lt; (mCurItem - pageLimit)) {             for (int i=0; i&lt;mItems.size(); i++) {                 mItems.get(i).scrolling = true;             }         }         final boolean dispatchSelected = mCurItem != item;          if (mFirstLayout) {             mCurItem = item;             if (dispatchSelected && mOnPageChangeListener != null) {                 mOnPageChangeListener.onPageSelected(item);             }             if (dispatchSelected && mInternalPageChangeListener != null) {                 mInternalPageChangeListener.onPageSelected(item);             }             requestLayout();         } else {             populate(item);             scrollToItem(item, smoothScroll, velocity, dispatchSelected); \/\/ \u0417\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f \u0434\u043e y \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0438 \u0442\u0443\u0442 \u0436\u0435 \u0432\u044b\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438         }     } <\/code><\/pre>\n<p>  \u041c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0443 \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u043d\u0435\u0432\u0438\u0434\u0438\u043c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 ViewPager \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 \u043a\u0430\u043a \u0436\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043a\u0440\u043e\u043b\u043b\u0438\u043d\u0433\u0430 \u0434\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"java\">   \/\/ \u0437\u0430\u0439\u0434\u0435\u043c \u0432 SmoothScrolltTo \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0432\u044b\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438          int duration = 0; \/\/\u0432\u0440\u0435\u043c\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438         velocity = Math.abs(velocity); \/\/ \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c         if (velocity &gt; 0) {               duration = 4 * Math.round(1000 * Math.abs(distance \/ velocity));         } else {             final float pageWidth = width * mAdapter.getPageWidth(mCurItem);             final float pageDelta = (float) Math.abs(dx) \/ (pageWidth + mPageMargin);             duration = (int) ((pageDelta + 1) * 100);         }         duration = Math.min(duration, MAX_SETTLE_DURATION);          mScroller.startScroll(sx, sy, dx, dy, duration);  <\/code><\/pre>\n<p>  \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0439, \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0434\u043e\u0431\u0430\u0432\u043b\u044e \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u0441\u043a\u0440\u043e\u043b\u043b\u0438\u043d\u0433\u0430 \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043c\u0435\u0442\u043e\u0434 setCurrentItemInternal \u0438 smoothScrollTo \u0438\u0442\u0430\u043a \u043f\u0440\u0435\u0441\u0442\u0443\u043f\u0438\u043c<\/p>\n<pre><code class=\"java\">     public void setCurrentItem( int item, int duration){         if (mAdapter == null || mAdapter.getCount() &lt;= 0) {             setScrollingCacheEnabled(false);             return;         }          int oldCurrentPos = getCurrentItem(); \/\/ \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u044e          if(oldCurrentPos == item){ \/\/ \u0435\u0441\u043b\u0438 \u043d\u044b\u043d\u0435\u0448\u043d\u044f\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f             return;         }else{ \/\/ \u0438\u043d\u0430\u0447\u0435 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u043e\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c             setOffscreenPageLimit(Math.abs(oldCurrentPos - item));         }          if (item &lt; 0) {             item = 0;         } else if (item &gt;= mAdapter.getCount()) {             item = mAdapter.getCount() - 1;         }          final int pageLimit = mOffscreenPageLimit;         if (item &gt; (mCurItem + pageLimit) || item &lt; (mCurItem - pageLimit)) {             \/\/ We are doing a jump by more than one page.  To avoid             \/\/ glitches, we want to keep all current pages in the view             \/\/ until the scroll ends.             for (int i=0; i&lt;mItems.size(); i++) {                 mItems.get(i).scrolling = true;             }         }         final boolean dispatchSelected = mCurItem != item;          if (mFirstLayout) {             \/\/ We don't have any idea how big we are yet and shouldn't have any pages either.             \/\/ Just set things up and let the pending layout handle things.             mCurItem = item;             if (dispatchSelected && mOnPageChangeListener != null) {                 mOnPageChangeListener.onPageSelected(item);             }             if (dispatchSelected && mInternalPageChangeListener != null) {                 mInternalPageChangeListener.onPageSelected(item);             }             requestLayout();         } else {             populate(item);              final ItemInfo curItem = infoForPosition(item);             int destX = 0;             if(curItem != null){                 final int width = getClientWidth();                 destX = (int) (width * Math.max(mFirstOffset, Math.min(curItem.offset, mLastOffset)));             }              if(duration &gt; 0){                  int velocity = duration \/ Math.abs(oldCurrentPos - item);                  smoothScrollToWithDuration(destX, 0,  duration); \/\/ \u0441\u043a\u0440\u043e\u043b\u0438\u043c                 if(mOnPageChangeListener != null && curItem.position != item){                     mOnPageChangeListener.onPageSelected(item);                 }                  if (curItem.position != item && mInternalPageChangeListener != null) {                     mInternalPageChangeListener.onPageSelected(item);                 }              }         }     }       void smoothScrollToWithDuration(int x, int y, int duration){              if (getChildCount() == 0) {                 \/\/ Nothing to do.                 setScrollingCacheEnabled(false);                 return;             }             int sx = getScrollX();             int sy = getScrollY();             int dx = x - sx;             int dy = y - sy;             if (dx == 0 && dy == 0) {                 completeScroll(true);                 populate();                 setScrollState(SCROLL_STATE_IDLE);                 return;             }              setScrollingCacheEnabled(true);             setScrollState(SCROLL_STATE_SETTLING);              final int width = getClientWidth();             final int halfWidth = width \/ 2;             final float distanceRatio = Math.min(1f, 1.0f * Math.abs(dx) \/ width);              mScroller.startScroll(sx, sy, dx, dy, duration); \/\/ \u0441\u043a\u0440\u043e\u043b\u0438\u043c \u0434\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c             if(mScroller.isFinished()){                 setOffscreenPageLimit(DEFAULT_OFFSCREEN_PAGES); \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432             }              ViewCompat.postInvalidateOnAnimation(this);      }  <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0441\u0435<\/p>\n<p>  \u0425\u043e\u0447\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0447\u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043d\u043e \u0445\u043e\u0447\u0443 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c, \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0438\u0448\u0438\u0442\u0435 \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/191974\/\"> http:\/\/habrahabr.ru\/post\/191974\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0414\u043e\u0431\u0440\u044b\u0439 \u0434\u0435\u043d\u044c,<\/p>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u043c\u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u043d\u0435\u0440\u043d\u0443\u044e \u0433\u0430\u043b\u0435\u0440\u0435\u0439\u043a\u0443 \u0441 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0435\u0439, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0431\u044b\u043b\u0430 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0438 \u0441\u0430\u043c\u043e\u0439 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0435\u0439 \u0432 ViewPager \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0431\u044b\u043b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u044b\u0441\u0442\u0440\u044b\u0439, \u0438 \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0441 1 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 5 \u0442\u043e \u043d\u0435 \u0443\u0432\u0438\u0434\u0438\u0448\u044c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 3-4 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-191974","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/191974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191974"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/191974\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}