Android開發心得——viewpager調用setCurrentItem設頁面沒有滑動效果?我來幫你

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   os   ar   

本博文歡迎轉載,轉載請註明來自唔系好人之Android小碼農

最近做項目,遇到一個焦點圖的問題,而我,在這段時間可以說是viewpager的愛好者,雖然有很多優秀的開源項目,但是我還是想要研究一下,viewpager,怎麼自己做帶有滑動效果的焦點圖切換。

通過一輪的網路搜尋,最終研究到一個方法通過反射來擷取viewpager的內部屬性,然後設定他的滑動屬性。

說實話,對於我這種半路出家的和尚來說,反射是個什麼真的不懂,我也是聽同事說的,他這個反射的作用就是強制擷取了一個並不公開的內部屬性,並修改來達到目的。

好了,廢話不多說,先來看看我們要做什麼,首先是建立一個類繼承scroller,並重寫scroller,代碼大概如下:

 1     private int mDuration = 1500; 2  3     public FixedSpeedScroller(Context context) { 4         super(context); 5         // TODO Auto-generated constructor stub 6     } 7  8     public FixedSpeedScroller(Context context, Interpolator interpolator) { 9         super(context, interpolator);10         // TODO Auto-generated constructor stub11     }12 13     // public FixedSpeedScroller(Context context, Interpolator interpolator,14     // boolean flywheel) {15     // super(context, interpolator, flywheel);16     // // TODO Auto-generated constructor stub17     // }18 19     @Override20     public void startScroll(int startX, int startY, int dx, int dy, int duration) {21         // Ignore received duration, use fixed one instead22         super.startScroll(startX, startY, dx, dy, mDuration);23     }24 25     @Override26     public void startScroll(int startX, int startY, int dx, int dy) {27         // Ignore received duration, use fixed one instead28         super.startScroll(startX, startY, dx, dy, mDuration);29     }30 31     /**32      * @Description 設定滑動間隔33      * @author Created by qinxianyuzou on 2014-10-29.34      * @param duration35      */36     public void setDuration(int duration) {37         mDuration = duration;38     }

其中有一個關鍵的屬性是mDuration,這個是設定滑動的間隔,單位是毫秒(有經驗的其實都已經看懂了),我設定了一個外部方法用於設定這個屬性。

另外有一個初始化方法被我注釋了是因為這個方法最低支援到API11,而我的項目要相容到API8,所以注釋掉了。說到這個,真的要吐槽一下,現在誰還用2.3的作業系統?老爺機都已經裝上Android4.0以上的系統了。

在寫好這個類之後就是調用了,只要寫在你要調用的viewpager的activity裡面就可以,當然,你記得給viewpager初始化,上代碼

 1         Interpolator sInterpolator = new AccelerateInterpolator(); 2         try { 3             Field mScroller; 4             mScroller = ViewPager.class.getDeclaredField("mScroller"); 5             mScroller.setAccessible(true);  6             FixedSpeedScroller scroller = new FixedSpeedScroller(vp_circleSummary_teacher.getContext(), sInterpolator); 7             scroller.setDuration(1000); 8             mScroller.set(vp_circleSummary_teacher, scroller); 9         } catch (NoSuchFieldException e) {10         } catch (IllegalArgumentException e) {11         } catch (IllegalAccessException e) {12         }

原理不是很懂,大概就是有個大牛去看了viewpager的源碼,然後發現了field這個類是用於設定viewpager的滑動問題,於是就被用反射抽取了出來,並且設定。

在set Field這個屬性之前,記得設定間隔,不然就會變成上面那個scroller類的預設時間。

經過這麼設定一下之後,你在用settCurrentItem來設定頁面,那麼就會發現,你的viewpager已經不再像以前一樣,翻頁秒翻,而是帶了一個滑動的效果。

Android開發心得——viewpager調用setCurrentItem設頁面沒有滑動效果?我來幫你

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.