iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android ViewPager导航小圆点实现无限循环效果
  • 816
分享到

Android ViewPager导航小圆点实现无限循环效果

viewpager导航无限循环 2023-05-30 21:05:16 816人浏览 八月长安
摘要

之前用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文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作