效果图
主要的自定义类
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
/**
* android banner图
*/
public class RollHeaderView extends FrameLayout implements OnPageChangeListener {
private Context mContext;
private ViewPager mViewPager;
private LinearLayout mDotLl;
private List<Integer> mUrlList;
private List<ImageView> dotList = null;
private MyAdapter mAdapter = null;
private Handler mHandler = null;
private AutoRollRunnable mAutoRollRunnable = null;
private int prePosition = 0;
private HeaderViewClickListener headerViewClickListener;
public RollHeaderView(Context context) {
this(context, null);
}
public RollHeaderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RollHeaderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
initView();
initData();
initListener();
}
//初始化view
private void initView() {
View.inflate(mContext, R.layout.view_header, this);
mViewPager = (ViewPager) findViewById(R.id.vp);
mDotLl = (LinearLayout) findViewById(R.id.ll_dot);
//让banner的高度是屏幕的1/4
ViewGroup.LayoutParams vParams = mViewPager.getLayoutParams();
vParams.height = (int) (DisplayUtil.getMobileHeight(mContext) * 0.25);
mViewPager.setLayoutParams(vParams);
}
//初始化数据
private void initData() {
dotList = new ArrayList<>();
mAutoRollRunnable = new AutoRollRunnable();
mHandler = new Handler();
mAdapter = new MyAdapter();
}
private void initListener() {
mViewPager.addOnPageChangeListener(this);
}
/**
* 设置数据
*
* @param urlList
*/
public void setImgUrlData(List<Integer> urlList) {
this.mUrlList = urlList;
if (mUrlList != null && !mUrlList.isEmpty()) {
//清空数据
dotList.clear();
mDotLl.removeAllViews();
ImageView dotIv;
LinearLayout.LayoutParams params = new LinearLayout.