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 

转载于:https://www.cnblogs.com/hexihexi/p/6192593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值