您好,登錄后才能下訂單哦!
1、具體步驟
說(shuō)下大概實(shí)現(xiàn)步驟,一般我們有兩種,一種是viewpager+作為游標(biāo)的點(diǎn) 。另外一種是重寫(xiě)viewpager。
效果圖:
1.1 布局,直接viewpager+一個(gè)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 動(dòng)態(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 動(dòng)態(tài)add的點(diǎn),常規(guī)白點(diǎn):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>
選中為紅點(diǎn): 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動(dòng)態(tài)添加進(jìn)去圖片和游標(biāo)點(diǎn)。
/** * 初始化數(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); //有多少?gòu)垐D就放置幾個(gè)點(diǎn) 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); //這個(gè)是無(wú)線輪詢(xún)的關(guān)鍵 ll_container.getChildAt(0).setBackgroundResource(R.drawable.point_select); prePosition=0; }
1.4 viewpgaer綁定PagerAdapter,這樣就能滑動(dòng)照片并且無(wú)限滑了。
class Myadapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; // 要無(wú)限輪播 } @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 實(shí)現(xiàn)游標(biāo),就是滑動(dòng)圖片,下面的紅點(diǎn)也跟著變化。
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實(shí)現(xiàn)自動(dòng)輪詢(xún)。開(kāi)啟一個(gè)線程即可。
/** * 自動(dòng)輪詢(xú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);}});}}}
這樣就搞定了,僅僅提供一個(gè)思路。自己可以擴(kuò)展,例如重寫(xiě)viewpager,把功能封裝在內(nèi)部即可。
總結(jié)
以上所述是小編給大家介紹的Android使用viewpager實(shí)現(xiàn)自動(dòng)無(wú)限輪播圖,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。