Banner轮播图的无限循环轮播功能、手动滑动功能
原理说明:(假如5张图轮播)
轮播图实现:
1、在List中存好这5张图的View对象传到PagerAdapter中(List的数目为0到5)。
2、设置ViewPager一开始的起始位置为5*1000=5000,并用seletedBanner记录该位置。
3、在PagerAdapter中将其getCount方法返回Integer的最大值,让它向左向右滑都不会越界。
4、在PagerAdapter中必须对当前位置%5才能计算出当前对应List中的位置(如5002%5=2,也就是List[2]中的对象)。
5、ViewPager监听滚动事件,用seletedBanner记录新位置。
6、在onResume中开始轮播。
圆点实现:
1、在一个水平的布局中添加5个圆点View对象,并存入一个List中。
2、遍历圆点List,在ViewPager滚动回调中判断seletedBanner(当前位置)%5,则该得到的值为选中圆点,其他为未选中圆点。
步骤一:res/layout下编写布局xml文件:
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/vp_banner"
android:layout_width="match_parent"
android:layout_height="130dp" />
android:id="@+id/ly_indecation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
android:gravity="center"
android:orientation="horizontal" />
步骤二:Activity代码中实现:
1、变量声明:
//首页轮播
private ViewPager vp_banner;
//五张BannnerId
private int[] bannerId = {R.drawable.home_b