概述
應(yīng)用首頁(yè)的廣告輪播Banner
袁翁,一般都會(huì)使用ViewPager
來(lái)實(shí)現(xiàn),但是ViewPager
沒(méi)有輪播效果秒拔。
現(xiàn)成有這么幾種實(shí)現(xiàn)方案:
1.使用Integer.MAX_VALUE
,理論上很難達(dá)到邊界莫矗。
2.裝飾adapter
方式imbryk/LoopingViewPager@Github。
3.擴(kuò)展ViewPager
方式yanzm/LoopViewPager砂缩。
相關(guān)庫(kù)
輪播效果其實(shí)就是一個(gè)定時(shí)任務(wù)趣苏,可以用定時(shí)器,Handler
等梯轻。
Trinea/Android Auto Scroll ViewPager@Github
此庫(kù)通過(guò)handler
定時(shí)發(fā)送消息實(shí)現(xiàn)食磕,用的比較多,使用中感覺(jué)切換并非無(wú)縫喳挑。主要代碼如下:
/**
* scroll only once
*/
public void scrollOnce() {
PagerAdapter adapter = getAdapter();
int currentItem = getCurrentItem();
int totalCount;
if (adapter == null || (totalCount = adapter.getCount()) <= 1) {
return;
}
int nextItem = (direction == LEFT) ? --currentItem : ++currentItem;
if (nextItem < 0) {
if (isCycle) {
setCurrentItem(totalCount - 1, isBorderAnimation);
}
} else if (nextItem == totalCount) {
if (isCycle) {
setCurrentItem(0, isBorderAnimation);
}
} else {
setCurrentItem(nextItem, true);
}
}
這里是在最后或第一頁(yè)直接setCurrentItem()
,因此彬伦,并非無(wú)縫輪播效果。
和本文開(kāi)頭的幾個(gè)控件整合一下伊诵,即可方便 的實(shí)現(xiàn)我們想要無(wú)縫 的效果单绑。
基本實(shí)現(xiàn)
1.修改Trinea/Android Auto Scroll ViewPager@Github
改變首頁(yè)和最后一頁(yè)的跳轉(zhuǎn)設(shè)置。
/**
* scroll only once
*/
public void scrollOnce() {
PagerAdapter adapter = getAdapter();
int currentItem = getCurrentItem();
if (adapter == null || adapter.getCount() <= 1) {
return;
}
int nextItem = (direction == LEFT) ? --currentItem : ++currentItem;
setCurrentItem(nextItem, true);
}
2.實(shí)現(xiàn)方式選擇
第一種方式其實(shí)主要改變了adapter
中的index
曹宴,大體如下:
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % listviews.size() ;
//....
}
只是理論上的無(wú)限搂橙,需要自己處理position
。
第二種裝飾了adapter
笛坦,在前后各添加一個(gè)Item
区转,當(dāng)?shù)搅宋覀兲砑拥呐R界item
的時(shí)候,立即設(shè)置到正確的 position
版扩,主要方法如下:
LoopPagerAdapterWrapper
:
int toRealPosition(int position) {
int realCount = getRealCount();
if (realCount == 0)
return 0;
int realPosition = (position-1) % realCount;
if (realPosition < 0)
realPosition += realCount;
return realPosition;
}
這里废离,內(nèi)部封裝position
映射,不需要自己處理position
礁芦,和正常ViewPager
使用方式一樣蜻韭。
第三種悼尾,重寫擴(kuò)展ViewPager
繼承 ViewGroup
,使其item
無(wú)限肖方。需要自己處理position
闺魏,而且 和 JakeWharton/ViewPagerIndicator@Github搭配使用起來(lái)不是很方便,setViewPager
方法需要額外處理俯画。
綜上舷胜,使用第二種方式 擴(kuò)展性和 易用性都比較好。
Indicator
JakeWharton/ViewPagerIndicator@Github
這是一個(gè)比較全面的indicator
活翩,如果我們只需要簡(jiǎn)單的CircleIndicator
,可以抽取來(lái)使用翻伺。
優(yōu)秀相關(guān)開(kāi)源庫(kù):
THEONE10211024/CircleIndicator@Github:一個(gè)輕量的圓形指示器
ongakuer/CircleIndicator@Github:才用Drawable寫的圓形指示器
也可以看看本人站在巨人肩膀上完成的DrawableIndicator@Github:可以實(shí)時(shí)偏移材泄,切換動(dòng)畫,drawable資源吨岭。
效果圖:

測(cè)試
my_banner.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.bobomee.android.scrollloopviewpager.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/picslooper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.bobomee.android.drawableindicator.widget.DrawableIndicator
android:id="@+id/pageIndexor1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
app:indicator_height="15dp"
app:indicator_margin="15dp"
app:indicator_select_src="@drawable/select_drawable"
app:indicator_unselect_src="@drawable/unselect_drawable"
app:indicator_width="15dp" />
</RelativeLayout>
MainActivity:
AutoScrollViewPager viewPager = (AutoScrollViewPager) findViewById(R.id.picslooper1);
viewPager.setFocusable(true);
viewPager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager()));
BaseIndicator pageIndex = (BaseIndicator) findViewById(R.id.pageIndexor1);
pageIndex.setViewPager(viewPager);
viewPager.startAutoScroll();
最終效果圖
