Android使用viewpager實現(xiàn)自動無限輪播圖
1、具體步驟
說下大概實現(xiàn)步驟,一般我們有兩種,一種是viewpager+作為游標(biāo)的點 。另外一種是重寫viewpager。
效果圖:
1.1 布局,直接viewpager+一個viewgroup就好。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.maxence.viewpager.MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="220dp" > <android.support.v4.view.ViewPager android:id="@+id/vp_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 作為viewgroup 動態(tài) add 游標(biāo) --> <LinearLayout android:id="@+id/ll_container" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:orientation="horizontal" android:layout_alignParentBottom="true" ></LinearLayout> </RelativeLayout> </RelativeLayout>
1.2 動態(tài)add的點,常規(guī)白點:point_normal.xml。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dip" android:color="#ffffff"/> <solid android:color="#ffffff" /> </shape>
選中為紅點: point_select.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="#ff0000" /> <solid android:color="#ff0000"/> </shape>
1.3動態(tài)添加進(jìn)去圖片和游標(biāo)點。
/** * 初始化數(shù)據(jù) */ private void initData() { mContext = this; int[] i = new int[] { R.drawable.bg_lunbo1, R.drawable.bg_lunbo2, R.drawable.bg_lunbo3, R.drawable.bg_lunbo4 }; al = new ArrayList<ImageView>(); for (int x = 0; x < i.length; x++) { ImageView iv = new ImageView(mContext); iv.setBackgroundResource(i[x]); al.add(iv); View v=new View(mContext); v.setBackgroundResource(R.drawable.point_normal); //有多少張圖就放置幾個點 LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15); layoutParams.leftMargin = 30; ll_container.addView(v,layoutParams); } vp_pager.setAdapter(new Myadapter()); vp_pager.setOnPageChangeListener(this); vp_pager.setCurrentItem(al.size()*1000); //這個是無線輪詢的關(guān)鍵 ll_container.getChildAt(0).setBackgroundResource(R.drawable.point_select); prePosition=0; }
1.4 viewpgaer綁定PagerAdapter,這樣就能滑動照片并且無限滑了。
class Myadapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; // 要無限輪播 } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { int position1=position % al.size(); ImageView imageView = al.get(position1); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } }
1.5 實現(xiàn)游標(biāo),就是滑動圖片,下面的紅點也跟著變化。
vp_pager.setOnPageChangeListener(this); @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { int newPosition = position % al.size(); ll_container.getChildAt(newPosition).setBackgroundResource(R.drawable.point_select); ll_container.getChildAt(prePosition).setBackgroundResource(R.drawable.point_normal); prePosition=newPosition; }
1.6實現(xiàn)自動輪詢。開啟一個線程即可。
/** * 自動輪詢 */ private void pollint() { pThread = new PollThread(); pThread.start(); } class PollThread extends Thread{ @Override public void run() { while (poll){ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
runOnUiThread(new Runnable() {@Overridepublic void run() {vp_pager.setCurrentItem(vp_pager.getCurrentItem()+1);}});}}}
這樣就搞定了,僅僅提供一個思路。自己可以擴(kuò)展,例如重寫viewpager,把功能封裝在內(nèi)部即可。
總結(jié)
以上所述是小編給大家介紹的Android使用viewpager實現(xiàn)自動無限輪播圖,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android?ViewPager實現(xiàn)一個無限輪播圖
- viewpager實現(xiàn)自動循環(huán)輪播圖
- Android Viewpager實現(xiàn)無限循環(huán)輪播圖
- ViewPager打造輪播圖Banner/引導(dǎo)頁Guide
- Android 使用ViewPager實現(xiàn)輪播圖效果
- 淺談Viewpager和輪播圖的沖突解決方法
- Android ViewPager實現(xiàn)輪播圖效果
- Android實現(xiàn)基于ViewPager的無限循環(huán)自動播放帶指示器的輪播圖CarouselFigureView控件
- Android中用RxJava和ViewPager實現(xiàn)輪播圖
- 使用ViewPager2實現(xiàn)簡易輪播圖效果
相關(guān)文章
Android自定義View實現(xiàn)天氣預(yù)報折線圖
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)天氣預(yù)報折線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09android開發(fā)仿ios的UIScrollView實例代碼
下面小編就為大家分享一篇android開發(fā)仿ios的UIScrollView實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android強(qiáng)制下線功能實現(xiàn)的代碼示例
本篇文章主要介紹了Android強(qiáng)制下線功能實現(xiàn)的代碼示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字
本文主要介紹了Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android應(yīng)用獲取設(shè)備序列號的方法
本篇文章主要介紹了Android應(yīng)用獲取設(shè)備序列號的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Android Studio開發(fā)中Gradle各種常見報錯問題解決方案
這篇文章主要為大家介紹了Android Studio開發(fā)中Gradle各種常見報錯問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12