您好,登錄后才能下訂單哦!
之前封裝過一個,但總覺得不夠優雅,就有了這個通用封裝,很簡潔,不知道夠不夠優雅,不過原來那個有跟隨指示器和絲滑滑動效果,感興趣可以看一下。
封裝過程
1、自定義屬性
selectPoint:選中指示器圖標
normalPoint:未選中指示器圖標
pointWidth:圖標寬度
switchTime:輪播間隔事件
location:指示器位置,下中或下右
<declare-styleable name="NewBannerView"> <attr name="selectPoint" format="reference" /> <attr name="normalPoint" format="reference" /> <attr name="pointWidth" format="dimension" /> <attr name="switchTime" format="integer" /> <attr name="location"> <enum name="CENTER" value="0" /> <enum name="RIGHT" value="1" /> </attr> </declare-styleable>
2、初始化View
初始化ViewPager和指示器組合View
3、綁定數據源
通過setImageData設置輪播圖數據源
4、綁定點擊事件
通過OnPageClickListener綁定點擊事件
5、開啟關閉輪播
start和stop方法開啟和關閉輪播
用法
xml中
<com.goldou.lovesee.view.NewBannerView android:id="@+id/bannerView" android:layout_width="match_parent" app:selectPoint="@drawable/red_point" app:normalPoint="@drawable/gray_point" app:switchTime="4000" app:location="RIGHT" android:layout_height="200dp" />
activity中
int[] imageList = {R.drawable.me_top, R.drawable.me_top, R.drawable.me_top, R.drawable.me_top}; NewBannerView bannerView = view.findViewById(R.id.bannerView); bannerView.setImageData(imageList); bannerView.start(); bannerView.setOnPageClickListener(new NewBannerView.OnPageClickListener() { @Override public void onPageClick(int position) { Toast.makeText(getActivity(), position + "", Toast.LENGTH_SHORT).show(); } });
BannerView
public class NewBannerView extends RelativeLayout implements View.OnClickListener { private Context context; private int selectPoint, normalPoint; private float pointWidth = 7; private int location; private int CENTER = 0, RIGHT = 1; private int lastPosition = 0; private ViewPager viewPager; private int switchTime = 5000; private int[] images; private OnPageClickListener onPageClickListener; private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what == 101) { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); start(); } return false; } }); public NewBannerView(Context context) { this(context, null); } public NewBannerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public NewBannerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; initAttr(attrs); } private void initAttr(AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NewBannerView); selectPoint = array.getResourceId(R.styleable.NewBannerView_selectPoint, R.mipmap.ic_launcher_round); normalPoint = array.getResourceId(R.styleable.NewBannerView_normalPoint, R.mipmap.ic_launcher_round); pointWidth = array.getDimension(R.styleable.NewBannerView_pointWidth, pointWidth); location = array.getInteger(R.styleable.NewBannerView_location, RIGHT); switchTime = array.getInteger(R.styleable.NewBannerView_switchTime, switchTime); array.recycle(); } public void setImageData(final int[] images) { this.images = images; final LinearLayout ll_point = new LinearLayout(context); LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.addRule(ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); layoutParams.bottomMargin = 20; if (location == CENTER) { layoutParams.addRule(CENTER_HORIZONTAL, RelativeLayout.TRUE); } else { layoutParams.addRule(ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); layoutParams.rightMargin = 20; } LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(UIUtil.dip2px(pointWidth), UIUtil.dip2px(pointWidth)); LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(UIUtil.dip2px(pointWidth), UIUtil.dip2px(pointWidth)); params1.leftMargin = 0; params2.leftMargin = UIUtil.dip2px(pointWidth); for (int i = 0; i < images.length; i++) { ImageView point = new ImageView(context); if (i == 0) { point.setBackgroundResource(selectPoint); point.setLayoutParams(params1); } else { point.setBackgroundResource(normalPoint); point.setLayoutParams(params2); } ll_point.addView(point); } viewPager = new ViewPager(context); viewPager.setAdapter(new BannerAdapter()); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { position = position % images.length; if (lastPosition == position) { return; } ll_point.getChildAt(position).setBackgroundResource(selectPoint); ll_point.getChildAt(lastPosition).setBackgroundResource(normalPoint); lastPosition = position; } @Override public void onPageScrollStateChanged(int state) { } }); LayoutParams layoutParams1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); addView(viewPager, layoutParams1); addView(ll_point, layoutParams); } public void start() { handler.sendEmptyMessageDelayed(101, switchTime); } public void stop() { handler.removeCallbacksAndMessages(null); } private int getCurrentItem() { return viewPager.getCurrentItem() % images.length; } @Override public void onClick(View view) { onPageClickListener.onPageClick(getCurrentItem()); } public interface OnPageClickListener { void onPageClick(int position); } public void setOnPageClickListener(OnPageClickListener onPageClickListener) { this.onPageClickListener = onPageClickListener; } private class BannerAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { position = position % images.length; ImageView imageView = new ImageView(context); imageView.setImageResource(images[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setOnClickListener(NewBannerView.this); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。