之前用View Pager做了一个图片切换的推荐栏(就类似与淘宝、头条客户端顶端的推荐信息栏),利用View Pager很快就能实现,但是一次无意间使用淘宝APP的时候,突然发现它的效果和我做的还不一样,淘宝APP的推荐栏可以左右无限循环切
之前用View Pager做了一个图片切换的推荐栏(就类似与淘宝、头条客户端顶端的推荐信息栏),利用View Pager很快就能实现,但是一次无意间使用淘宝APP的时候,突然发现它的效果和我做的还不一样,淘宝APP的推荐栏可以左右无限循环切换,而ViewPager自身其实并没有支持这个功能。
其实实现这个无限循环不难,只需要在数据源的首尾各添加一张多余的图片,在onPagerChangeListener()中监听position<1和position>(总数据条目-1)就可以了。另外一点需要注意的是,这里的数据源+2,而导航小圆点却比数据源少2,这样在无限循环的时候,小圆点的切换就不好办了。本人最开始也是写逻辑在onPageSelected()里面判断条件,总感觉挺麻烦的,有没有更好的实现方式呢。答案是肯定的。只需将小圆点也首尾各家一个,并设置为invisible不就好了?
我的代码实现如下:
xml布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:Android="Http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp_homepage" android:layout_width="match_parent" android:layout_height="match_parent" ></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/ll_dots_homepage_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="8dp" android:gravity="center" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:visibility="invisible" android:src="@drawable/dots"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:src="@drawable/dots"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:src="@drawable/dots"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:src="@drawable/dots"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:src="@drawable/dots"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:padding="5dp" android:visibility="invisible" android:src="@drawable/dots"/> </LinearLayout></RelativeLayout>
--结束END--
本文标题: Android ViewPager导航小圆点实现无限循环效果
本文链接: https://www.lsjlt.com/news/221534.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0