1、具体步骤
说下大概实现步骤,一般我们有两种,一种是viewpager+作为游标的点 。另外一种是重写viewpager。
效果图:
1.1 布局,直接viewpager+一个viewgroup就好。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<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 动态 add 游标 --> <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 动态add的点,常规白点:point_normal.xml。
1
2
3
4
5
6
7
8
|
<?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
1
2
3
4
5
6
7
8
9
|
<?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动态添加进去图片和游标点。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 初始化数据 */ 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 ); //这个是无线轮询的关键 ll_container.getChildAt( 0 ).setBackgroundResource(R.drawable.point_select); prePosition= 0 ; } |
1.4 viewpgaer绑定PagerAdapter,这样就能滑动照片并且无限滑了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 实现游标,就是滑动图片,下面的红点也跟着变化。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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实现自动轮询。开启一个线程即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * 自动轮询 */ 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);}});}}}
这样就搞定了,仅仅提供一个思路。自己可以扩展,例如重写viewpager,把功能封装在内部即可。
总结
以上所述是小编给大家介绍的Android使用viewpager实现自动无限轮播图,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/u012560369/article/details/80619573