轮播图主要用于广告的展示,在开发过程中很常用,之前项目上也是参考了网上资料做的(具体哪个资料找不着了),效果还是不错的。最近一个同事不知道从网上找了个轮播图,直接用到了新项目上,把sdk最低改成了19,这哪成。我就看了下依赖的这个项目,写的实在不怎么样,肯定不能直接依赖的,在其基础上做些优化倒是可以的。于是,我就把之前项目上轮播图整理了下。
gitHub托管地址:https://github.com/dingfengnupt/SlideView
项目依赖:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.dingfengnupt:SlideView:1.0.3'
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.windy.slideview.SlideViewPager
android:id="@+id/slideViewPager"
android:layout_width="wrap_content"
android:layout_height="200dp"></com.windy.slideview.SlideViewPager>
</LinearLayout>
public class MyAdapter extends RecyclingPagerAdapter {
private Context mContext;
private String[] mUrls;
private int size;
private boolean isInfiniteLoop = false;
public MyAdapter(Context context, String[] urls) {
this.mContext = context;
this.mUrls = urls;
this.size = urls.length;
}
@Override
public int getCount() {
return isInfiniteLoop ? Integer.MAX_VALUE : mUrls.length;
}
// get real position
private int getPosition(int position) {
return isInfiniteLoop ? position % size : position;
}
public boolean isInfiniteLoop() {
return isInfiniteLoop;
}
/**
* @param isInfiniteLoop
*/
public void setInfiniteLoop(boolean isInfiniteLoop) {
this.isInfiniteLoop = isInfiniteLoop;
}
@Override
public View getView(final int position, View convertView, ViewGroup container) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pager, null);
holder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Glide.with(mContext).load(mUrls[getPosition(position)]).into(holder.image);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "onClick: " + getPosition(position), Snackbar.LENGTH_SHORT).show();
}
});
return convertView;
}
private static class ViewHolder {
ImageView image;
}
}
继承
RecyclingPagerAdapter
实现,设置count为无限大,用于循环。这个是例子上的做法,可根据自身需求实现,跟PagerAdapter一样的用。
代码中设置
mSlideViewPager = (SlideViewPager) findViewById(R.id.slideViewPager);
mSlideViewPager.setIndicator(mUrls.length);
MyAdapter adapter = new MyAdapter(this, mUrls);
adapter.setInfiniteLoop(true);
mSlideViewPager.setAdapter(adapter);
其中
setIndicator
和
setAdapter必须执行,setAdapter好理解,setIndicator用来设置指示图标。
自定义设置
轮播时间:默认是3s,
mSlideViewPager.setInterval(5000);
指示图标:默认是黑白圆点,
mSlideViewPager.setIndicator(mUrls.length, R.drawable.indicator);
指示图标资源文件使用selector样式,如,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selected" android:state_selected="true" />
<item android:drawable="@drawable/unselected" />
</selector>