首页 > 代码库 > Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
- 自定义ADPager
自定义水平滚动的ScrollView效仿ViewPager
当遇到要在ViewPager中添加多张网络请求图片的情况下,不能进行复用,导致每次都要重新去求情已经请求过的数据致使流量数据过大
自定义的数据结构解决了这个问题,固定传递的图片数据之后进行统一请求,完成后进行页面切换数据复用
代码中涉及网络请求是用的Volley网络请求框架
PicCarousel是网络数据请求的URL相关数据(使用者自己需要的URL)
public class ADPager extends HorizontalScrollView implements View.OnClickListener{
private final int VELOCITY_SLOT = 1000;
private final int DEFAULT_AUTOPLAY_DURATION = 5000;
private List<PicCarousel> noticeList;
private LinearLayout container;
private LinearLayout.LayoutParams linearLayoutParams;
private ImageLoader mImageLoader;
// private DisplayImageOptions imageOptions;
private VelocityTracker velocityTracker;
private OnADPageClickListener mADPageClickListener;
private int mCurrPage = 0;
private long mDuration = DEFAULT_AUTOPLAY_DURATION;
private boolean mIsAutoPlaying = false;
private AutoPlayRunnable mAutoPlayRunnable = new AutoPlayRunnable();
private int mMaximumVelocity;
private float mCircleRadius;
private Paint mStrokePaint;
private Paint mFillPaint;
public ADPager(Context context) {
super(context);
init();
}
public ADPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ADPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
Context ctx = getContext();
this.container = new LinearLayout(ctx);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.container.setOrientation(LinearLayout.HORIZONTAL);
this.container.setLayoutParams(params);
this.addView(this.container);
this.linearLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
this.linearLayoutParams.weight = 1; // 平等分
this.noticeList = new ArrayList<>();
this.setHorizontalScrollBarEnabled(false);
// this.imageLoader = ImageLoader.getInstance();
mImageLoader = new com.android.volley.toolbox.ImageLoader(SingleRequestQueue.getRequestQueue(Utils.getContext()), new BitmapCache());
this.setSmoothScrollingEnabled(true);
final Resources res = getResources();
this.mCircleRadius = 8;
/** 默认图 **/
NetworkImageView imgView = makeImageView();
this.container.addView(imgView);
/** 默认图结束 **/
/**
* 设置松手时velocity的追踪
*/
final ViewConfiguration configuration = ViewConfiguration.get(ctx);
this.mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
// DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
// builder.cacheInMemory(true).cacheOnDisc(true)
// .showImageForEmptyUri(R.mipmap.def_pic)
// .showImageOnLoading(R.mipmap.def_pic)
// .showImageOnFail(R.mipmap.def_pic);
// imageOptions = builder.build();
initPaint();
}
private void initPaint() {
mStrokePaint = new Paint();
mStrokePaint.setStrokeWidth(1.0f);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(Color.WHITE);
mStrokePaint.setAntiAlias(true);
mFillPaint = new Paint();
mFillPaint.setColor(Color.WHITE);
mFillPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mFillPaint.setAntiAlias(true);
}
private NetworkImageView makeImageView() {
Context