一、组件介绍 |
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
作者:杰瑞教育 出处: 版权声明:本文版权归烟 台 科 技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 技术咨询: