2015-12-28 15:26 by 杰瑞教育, 32 阅读, 0 评论, 收藏, 编辑html
1、组件介绍 |
App产品中信息列表头部都会有自动轮转的广告图片,使用ViewPager能够实现但编码比较麻烦,咱们能够采用使用LoopView开源控件来完成, LoopView是一个强大的轮转大图控件,而且提供了许多配置方法足以知足你的应用需求android
2、环境配置 |
若是您的项目使用 Gradle 构建, 只须要在您的build.gradle文件添加下面一行到 dependencies :json
compile 'com.kevin:loopview:1.0.4'app
3、如何使用 |
一、在layout.xml 中配置LoopViewide
在Layout文件添加<com.kevin.loopview.AdLoopView>代码以下:oop
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:kevin="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <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" kevin:loop_dotSelector="@drawable/ad_dots_selector" kevin:loop_layout="@layout/ad_loopview_layout"> </com.kevin.loopview.AdLoopView> </RelativeLayout>
二、在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(); } }
三、所涉及LocalFileUtils的主要方法post
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(); } } } } }
4、LoopView主要方法 |
// 设置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);
做者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/
gradle