ViewPager跨距離切換過渡動畫異常
今天在使用ViewPager時,因功能需求需要使用跨距離(跨越多頁悠栓,與當前頁相距>1頁按价。如0至10)切換楼镐,代碼也非常簡單,通過ViewPager的setCurrentItem(int item)
進行切換框产,然后就出現(xiàn)了上述的問題過渡動畫閃瞎登場:
我的代碼:
setCurrentItem(int item).png
運行效果:
before.gif
我的眼睛.jpg
解決方法
-
使用setCurrentItem(int item,boolean smoothScroll)方法
在跨距離切換的時候盾舌,使用setCurrentItem(position,false)
方法來操作即可解決問題蘸鲸。- 參數(shù)item:目標頁的位置;
- 參數(shù)smoothScroll:是否平滑過渡(true:是膝舅,false:否)窑多。
-
在跨距離切換時控制ViewPager.mScroller的滾動時長
如果上述方法并不適用您的應用場景或者還存在其它問題,我們還可以通過自定義ViewPager的Scroller的方式來控制動畫時長埂息,從而解決問題。
自定義一個Scroller享幽,增加設置無完成滾動時間方法:
class XScroller extends Scroller {
private boolean noDuration = false;// 標識是否沒有滾動時間(true:沒有拾弃;false:有),默認為有
XScroller(Context context) {
super(context);
}
public XScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public XScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
/**
* 開始滾動
*
* @param startX 開始X位置
* @param startY 開始Y位置
* @param dx 目標X位置
* @param dy 目標Y位置
* @param duration 完成滾動的時間
*/
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
if (noDuration) {// 跨距離切換時携栋,不需要有完成滾動時間延遲
duration = 0;
}
super.startScroll(startX, startY, dx, dy, duration);
}
void setNoDuration(boolean noDuration) {
this.noDuration = noDuration;
}
}
在ViewPager中咳秉,通過反射設置mScroller域為自己定義的xScroller:
xScroller = new XScroller(context);
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
field.set(this, xScroller );// 利用反射設置mScroller域為自己定義的xScroller
} catch (Exception e) {
Log.e(TAG, "setViewPagerScrollSpeed error:" + e.toString());
}
重寫setCurrentItem方法,在里面加一層判斷磅摹,決定是否設置滾動時間為0:
/**
* 設置切換到當前選擇的頁面
*
* @param item 選定頁面的下標
* @param smoothScroll 是否平滑滾動
*/
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
int current = getCurrentItem();
// 如果頁面相隔大于1,就設置頁面切換時完成滑動的時間為0
if (Math.abs(current - item) > 1) {
loopScroller.setNoDuration(true);// 滑動前設置動畫時間為0
super.setCurrentItem(item, smoothScroll);
loopScroller.setNoDuration(false);// 滑動結束后設置動畫時間恢復
} else {
loopScroller.setNoDuration(false);
super.setCurrentItem(item, smoothScroll);
}
}
運行效果:
after.gif
問題分析
碰到這個問題大家可能會比較郁悶,為什么我沒有設置過渡動畫幕侠,且只用了setCurrentItem(int item)方法卻出現(xiàn)了這種情況碍彭?
- setCurrentItem(int item)源碼分析
/**
* Set the currently selected page. If the ViewPager has already been through its first
* 設置切換到當前選定頁。如果ViewPager已經(jīng)通過其與當前適配器的第一個布局
* layout with its current adapter there will be a smooth animated transition between
* 將有一個平滑的動畫過渡當前item和指定item之間舞箍。
* the current item and the specified item.
*
* @param item Item index to select
*/
public void setCurrentItem(int item) {
mPopulatePending = false;
setCurrentItemInternal(item, !mFirstLayout, false);
}
也就是說皆疹,ViewPager在通過其與當前適配器的第一個布局后略就,在當前item與指定item切換時,還是會存在一個平滑的過渡動畫表牢,這也是我們多頁面切換時出現(xiàn)問題的病癥所在,這里主要是由
mFirstLayout
來控制——源碼中的mFirstLayout
默認值為true彰导,在onLayout
方法中又變動其值為false.
-
setCurrentItem(int item, boolean smoothScroll)源碼分析
那么如果使用ViewPager類中與setCurrentItem(int item)
比較相似的setCurrentItem(int item, boolean smoothScroll)
呢敲茄?我們先來看下它的源碼:
/**
* Set the currently selected page.
* 設置切換到當前選擇的頁面
* @param item Item index to select 選定頁面的下標
* @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
* true:平滑滾動到新的item折汞,false:立即滾動到指定位置.
*/
public void setCurrentItem(int item, boolean smoothScroll) {
mPopulatePending = false;
setCurrentItemInternal(item, smoothScroll, false);
}
通過對
setCurrentItem(int item, boolean smoothScroll)
源碼的分析,我們可以發(fā)現(xiàn)损同,當我們使用其代替setCurrentItem(int item)
進行使用時,為了避免跨距離切換出現(xiàn)上述異常情況茂卦,我們只需要設置smoothScroll = false组哩,關閉過渡動畫即可解決。
如果您有更好的解決方案歡迎評論分享蛛砰,如有錯誤黍衙,請批評指正,謝謝位仁。