博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 开源组件 ----- Android LoopView无限自动轮转控件
阅读量:7056 次
发布时间:2019-06-28

本文共 3638 字,大约阅读时间需要 12 分钟。

一、组件介绍

  App产品中信息列表头部都会有自动轮转的广告图片,使用ViewPager可以实现但编码比较麻烦,我们可以采用使用LoopView开源控件来完成, LoopView是一个强大的轮转大图控件,并且提供了许多配置方法足以满足你的应用需求

二、环境配置

  如果您的项目使用 Gradle 构建, 只需要在您的build.gradle文件添加下面一行到 dependencies :

  compile 'com.kevin:loopview:1.0.4'

三、如何使用

1、在layout.xml 中配置LoopView

  在Layout文件添加<com.kevin.loopview.AdLoopView>代码如下:

2、在Activity添加代码:

public class AdLoopActivity extends Activity implements BaseLoopAdapter.OnItemClickListener{    AdLoopView mLoopView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_adloopview);        initViews();        initEvents();    }    private void initViews() {        mLoopView = (AdLoopView)                         this.findViewById(R.id.adloop_act_adloopview);        initRotateView();    }    /**     * 初始化LoopView     */    private void initRotateView() {        // 设置自定义布局//        mLoopView.setLoopLayout(R.layout.ad_loopview_layout);        // 设置数据          String json = LocalFileUtils.getStringFormAsset(this,                                 "loopview_date.json");        LoopData loopData = JsonTool.toBean(json, LoopData.class);        if(null != loopData) {            mLoopView.refreshData(loopData);        }        // 设置页面切换过度事件        mLoopView.setScrollDuration(2000);        // 设置页面切换时间间隔        mLoopView.setInterval(3000);    }    /**     * 初始化事件*/    private void initEvents() {        mLoopView.setOnClickListener(this);    }    @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);    }    @Override    protected void onDestroy() {        super.onDestroy();    }}

3、所涉及LocalFileUtils的主要方法

public class LocalFileUtils {        /**     * 获取Asset下文本内容     */public static String getStringFormAsset(Context context, String str) {        BufferedReader in = null;        try {      in = new BufferedReader(new InputStreamReader(context.getAssets().open(str)));            String line;            StringBuilder buffer = new StringBuilder();            while ((line = in.readLine()) != null) {                buffer.append(line).append('\n');            }            return buffer.toString();        } catch (IOException e) {            e.printStackTrace();            return "";        } finally {            if (in != null) {                try {                    in.close();                    in = null;                } catch (IOException e) {                    e.printStackTrace();                }            }        }}}
四、LoopView主要方法
// 设置ViewPager页面切换时间mLoopView.setScrollDuration(1000);// 设置轮转时间间隔mLoopView.setInterval(3000);// 以集合的方式初始化数据mLoopView.setLoopViewPager(List
> data);// 以JSON的方式初始化数据mLoopView.setLoopViewPager(String jsonData);// 以数据实体的方式初始化数据mLoopView.setLoopViewPager(LoopData rotateData);// 以集合的方式刷新数据mLoopView.refreshData(final List
> data);// 以数据实体的方式刷新数据mLoopView.refreshData(LoopData loopData);// 以JSON的方式刷新数据mLoopView.refreshData(String jsonData);// 获取配置的轮转大图数据mLoopView.getLoopData();// 开始自动轮转mLoopView.startAutoLoop();// 在指定时间延迟后自动轮转mLoopView.startAutoLoop(long delayTimeInMills);// 停止自动轮转mLoopView.stopAutoLoop();// 设置自定义布局mLoopView.setLoopLayout(int layoutResId);

 

作者:杰瑞教育
出处:
 
版权声明:本文版权归
技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:
 
你可能感兴趣的文章
easyui 合并单元格
查看>>
数据库的易犯的错误
查看>>
C++虚函数及虚函数表解析
查看>>
仿网易漂亮的TAB选项卡(标签)
查看>>
ylb:SQL Server中的时间函数
查看>>
【入门必备】最佳的 Node.js 学习教程和资料书籍
查看>>
UML解惑:图说UML中的六大关系--转
查看>>
ios开发FMDB导入SQLCipher加密数据库
查看>>
为什么你的代码如此难以理解(转)
查看>>
php安全编程—sql注入攻击
查看>>
JSP简单的练习-功能标签
查看>>
TreeMap cannot be cast to java.lang.Comparable
查看>>
Eclipse和PyDev搭建完美Python开发环境(Windows篇)
查看>>
Extjs4.2布局——layout: accordion(Ext.layout.container.Accordion)
查看>>
OC ARC之循环引用问题(代码分析)
查看>>
Windows/Centos安装GO语言环境
查看>>
C/C++函数指针声明
查看>>
iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
查看>>
24款最好的jQuery日期时间选择器插件
查看>>
记2016腾讯 TST 校招面试经历,电面、笔试写代码、技术面、hr面,共5轮
查看>>