| 一、组件介绍 |
App产品中信息列表头部都会有自动轮转的广告图片,使用ViewPager可以实现但编码比较麻烦,我们可以采用使用LoopView开源控件来完成, LoopView是一个强大的轮转大图控件,并且提供了许多配置方法足以满足你的应用需求
| 二、环境配置 |
如果您的项目使用 Gradle 构建, 只需要在您的build.gradle文件添加下面一行到 dependencies :
compile 'com.kevin:loopview:1.0.4'
| 三、如何使用 |
1、在layout.xml 中配置LoopView
在Layout文件添加<com.kevin.loopview.AdLoopView>代码如下:
<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>
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<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/
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:
出处:http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:

来源:https://www.cnblogs.com/jerehedu/p/5082687.html