Android LoopViewPager使用案例

本文介绍了Android LoopViewPager的使用,包括添加权限、依赖、配置属性和代码示例。LoopViewPager是一个页面轮播控件,支持自定义,并提供自动轮播功能。详细内容涵盖了XML和Java代码的实现方式,以及运行效果、最低支持版本和问题修复。
摘要由CSDN通过智能技术生成

LoopViewPager

Android LoopViewPager 页面轮播控件
https://github.com/open-android/LoopViewPager

添加权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

添加依赖

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    compile 'com.github.open-android:loopviewpager:1.0.0'
}

可配置的属性

    <declare-styleable name="LoopViewPager">
        <!-- 轮播时间,默认值0表示不自动轮播 -->
        <attr name="loopTime" format="integer" />
        <!-- 动画时间 -->
        <attr name="animTime" format="integer" />
        <!-- 动画效果 -->
        <attr name="animStyle" format="enum">
            <!-- 折叠效果 -->
            <enum name="accordion" value="1" />
            <enum name="accordionUp" value="2" />
            <!-- 立方体效果 -->
            <enum name="cube" value="3" />
            <enum name="cubeUp" value="4" />
        </attr>
        <!-- 是否可以手动滚动页面,默认true -->
        <attr name="scrollEnable" format="boolean" />
        <!-- 触摸页面是否停止轮播,默认true -->
        <attr name="touchEnable" format="boolean" />
    </declare-styleable>
    <declare-styleable name="LoopDotsView">
        <!-- 圆点大小 -->
        <attr name="dotSize" format="integer|dimension|reference" />
        <!-- 圆点宽度 -->
        <attr name="dotWidth" format="integer|dimension|reference" />
        <!-- 圆点高度 -->
        <attr name="dotHeight" format="integer|dimension|reference" />
        <!-- 圆点距离 -->
        <attr name="dotRange" format="integer|dimension|reference" />
        <!-- 圆点形状 -->
        <attr name="dotShape">
            <!-- 矩形,默认值 -->
            <enum name="rectangle" value="1" />
            <!-- 圆形 -->
            <enum name="oval" value="2" />
            <!-- 三角形 -->
            <enum name="triangle" value="3" />
            <!-- 菱形 -->
            <enum name="diamond" value="4" />
        </attr>
        <!-- 圆点颜色,默认值0xFFFFFFFF -->
        <attr name="dotColor" format="color|reference" />
        <!-- 圆点选中颜色,默认值0x55000000 -->
        <attr name="dotSelectColor" format="color|reference" />
        <!-- 圆点资源 -->
        <attr name="dotResource" format="reference" />
        <!-- 圆点选中资源 -->
        <attr name="dotSelectResource" format="reference" />
    </declare-styleable>

配置说明

  • 1、loopTime的值应不小于1s,如果值为0,则不自动轮播;
  • 2、animTime的值应小于loopTime;
  • 3、dotWidth、dotHeight、dotRange如果没有设置,则默认值为dotSize;
  • 4、如果设置了dotResource、dotSelectResource,则dotShape、dot
LoopView 是一个强大的轮转大图控件,并且提供了许多配置方法来达到您的显示效果和需求。github地址:https://github.com/xuehuayous/Android-LoopView介绍博客地址:http://blog.csdn.net/xuehuayous/article/details/50518393在项目中使用 LoopView如果您的项目使用 Gradle 构建, 只需要在您的build.gradle文件添加下面一行到 dependencies :    compile 'com.kevin:loopview:1.0.5'简单使用在layout.xml 中配置LoopView在Layout文件添加<com.kevin.loopview.AdLoopView<com.kevin.loopview.AdLoopView     android:id="@ id/main_act_adloopview"     android:layout_width="match_parent"     android:layout_height="192dp"> </com.kevin.loopview.AdLoopView>在代码中配置AdLoopView mLoopView = (AdLoopView) this.findViewById(R.id.main_act_adloopview); String json = LocalFileUtils.getStringFormAsset(this, "loopview_date.json"); // 使用 JsonTool 封装 JSON 数据到实体对象 LoopData loopData = JsonTool.toBean(json, LoopData.class); // 通过对象的方式设置数据 mLoopView.refreshData(loopData); // 开始轮转 mLoopView.startAutoLoop(); // 设置点击监听 mLoopView.setOnClickListener(new BaseLoopAdapter.OnItemClickListener() {         @Override         public void onItemClick(PagerAdapter parent, View view,              int position, int realPosition) {             // 获取数据             LoopData loopData = mLoopView.getLoopData();             String url = loopData.items.get(position).link;             // 通过系统浏览器打开跳转链接             Intent intent = new Intent();             intent.setData(Uri.parse(url));             intent.setAction(Intent.ACTION_VIEW);             startActivity(intent);         }     });更多配置XML 配置在XML中使用AdLoopView,可以有如下配置:<com.kevin.loopview.AdLoopView     android:id="@ id/adloop_act_adloopview"     android:layout_width="match_parent"     android:layout_height="192dp"     kevin:loop_interval="5000"     kevin:loop_dotMargin="5dp"     kevin:loop_autoLoop="[true|false]"     kevin:loop_dotSelector="@drawable/ad_dots_selector"     kevin:loop_layoutId="@layout/ad_loopview_layout"> </com.kevin.loopview.AdLoopView>在代码中配置// 设置ViewPager页面切换时间 mLoopView.setScrollDuration(1000); // 设置轮转时间间隔 mLoopView.setInterval(3000); // 以集合的方式初始化数据 mLoopView.setLoopViewPager(List<Map<String, String>> data); // 以JSON的方式初始化数据 mLoopView.setLoopViewPager(String jsonData); // 以数据实体的方式初始化数据 mLoopView.setLoopViewPager(LoopData rotateData); // 以集合的方式刷新数据 mLoopView.refreshData(final List<Map<String, String>> data); // 以数据实体的方式刷新数据 mLoopView.refreshData(LoopData loopData); // 以JSON的方式刷新数据 mLoopView.refreshData(String jsonData); // 获取配置的轮转大图数据 mLoopView.getLoopData(); // 开始自动轮转 mLoopView.startAutoLoop(); // 在指定时间延迟后自动轮转 mLoopView.startAutoLoop(long delayTimeInMills); // 停止自动轮转 mLoopView.stopAutoLoop(); // 设置自定义布局 mLoopView.setLoopLayout(int layoutResId);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值